xref: /openbmc/linux/net/smc/smc_close.c (revision 8a1e6bb3)
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 	/* Send out corked data remaining in sndbuf */
61 	smc_tx_pending(&smc->conn);
62 
63 	smc->wait_close_tx_prepared = 1;
64 	add_wait_queue(sk_sleep(sk), &wait);
65 	while (!signal_pending(current) && timeout) {
66 		int rc;
67 
68 		rc = sk_wait_event(sk, &timeout,
69 				   !smc_tx_prepared_sends(&smc->conn) ||
70 				   sk->sk_err == ECONNABORTED ||
71 				   sk->sk_err == ECONNRESET ||
72 				   smc->conn.killed,
73 				   &wait);
74 		if (rc)
75 			break;
76 	}
77 	remove_wait_queue(sk_sleep(sk), &wait);
78 	smc->wait_close_tx_prepared = 0;
79 }
80 
81 void smc_close_wake_tx_prepared(struct smc_sock *smc)
82 {
83 	if (smc->wait_close_tx_prepared)
84 		/* wake up socket closing */
85 		smc->sk.sk_state_change(&smc->sk);
86 }
87 
88 static int smc_close_wr(struct smc_connection *conn)
89 {
90 	conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
91 
92 	return smc_cdc_get_slot_and_msg_send(conn);
93 }
94 
95 static int smc_close_final(struct smc_connection *conn)
96 {
97 	if (atomic_read(&conn->bytes_to_rcv))
98 		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
99 	else
100 		conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;
101 	if (conn->killed)
102 		return -EPIPE;
103 
104 	return smc_cdc_get_slot_and_msg_send(conn);
105 }
106 
107 int smc_close_abort(struct smc_connection *conn)
108 {
109 	conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
110 
111 	return smc_cdc_get_slot_and_msg_send(conn);
112 }
113 
114 static void smc_close_cancel_work(struct smc_sock *smc)
115 {
116 	struct sock *sk = &smc->sk;
117 
118 	release_sock(sk);
119 	cancel_work_sync(&smc->conn.close_work);
120 	cancel_delayed_work_sync(&smc->conn.tx_work);
121 	lock_sock(sk);
122 }
123 
124 /* terminate smc socket abnormally - active abort
125  * link group is terminated, i.e. RDMA communication no longer possible
126  */
127 void smc_close_active_abort(struct smc_sock *smc)
128 {
129 	struct sock *sk = &smc->sk;
130 	bool release_clcsock = false;
131 
132 	if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) {
133 		sk->sk_err = ECONNABORTED;
134 		if (smc->clcsock && smc->clcsock->sk)
135 			tcp_abort(smc->clcsock->sk, ECONNABORTED);
136 	}
137 	switch (sk->sk_state) {
138 	case SMC_ACTIVE:
139 	case SMC_APPCLOSEWAIT1:
140 	case SMC_APPCLOSEWAIT2:
141 		sk->sk_state = SMC_PEERABORTWAIT;
142 		smc_close_cancel_work(smc);
143 		if (sk->sk_state != SMC_PEERABORTWAIT)
144 			break;
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 		if (sk->sk_state != SMC_PEERABORTWAIT)
154 			break;
155 		sk->sk_state = SMC_CLOSED;
156 		smc_conn_free(&smc->conn);
157 		release_clcsock = true;
158 		sock_put(sk); /* passive closing */
159 		break;
160 	case SMC_PROCESSABORT:
161 	case SMC_APPFINCLOSEWAIT:
162 		sk->sk_state = SMC_PEERABORTWAIT;
163 		smc_close_cancel_work(smc);
164 		if (sk->sk_state != SMC_PEERABORTWAIT)
165 			break;
166 		sk->sk_state = SMC_CLOSED;
167 		smc_conn_free(&smc->conn);
168 		release_clcsock = true;
169 		break;
170 	case SMC_INIT:
171 	case SMC_PEERABORTWAIT:
172 	case SMC_CLOSED:
173 		break;
174 	}
175 
176 	sock_set_flag(sk, SOCK_DEAD);
177 	sk->sk_state_change(sk);
178 
179 	if (release_clcsock) {
180 		release_sock(sk);
181 		smc_clcsock_release(smc);
182 		lock_sock(sk);
183 	}
184 }
185 
186 static inline bool smc_close_sent_any_close(struct smc_connection *conn)
187 {
188 	return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
189 	       conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
190 }
191 
192 int smc_close_active(struct smc_sock *smc)
193 {
194 	struct smc_cdc_conn_state_flags *txflags =
195 		&smc->conn.local_tx_ctrl.conn_state_flags;
196 	struct smc_connection *conn = &smc->conn;
197 	struct sock *sk = &smc->sk;
198 	int old_state;
199 	long timeout;
200 	int rc = 0;
201 	int rc1 = 0;
202 
203 	timeout = current->flags & PF_EXITING ?
204 		  0 : sock_flag(sk, SOCK_LINGER) ?
205 		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
206 
207 	old_state = sk->sk_state;
208 again:
209 	switch (sk->sk_state) {
210 	case SMC_INIT:
211 		sk->sk_state = SMC_CLOSED;
212 		break;
213 	case SMC_LISTEN:
214 		sk->sk_state = SMC_CLOSED;
215 		sk->sk_state_change(sk); /* wake up accept */
216 		if (smc->clcsock && smc->clcsock->sk) {
217 			smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
218 			smc->clcsock->sk->sk_user_data = NULL;
219 			rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
220 		}
221 		smc_close_cleanup_listen(sk);
222 		release_sock(sk);
223 		flush_work(&smc->tcp_listen_work);
224 		lock_sock(sk);
225 		break;
226 	case SMC_ACTIVE:
227 		smc_close_stream_wait(smc, timeout);
228 		release_sock(sk);
229 		cancel_delayed_work_sync(&conn->tx_work);
230 		lock_sock(sk);
231 		if (sk->sk_state == SMC_ACTIVE) {
232 			/* send close request */
233 			rc = smc_close_final(conn);
234 			sk->sk_state = SMC_PEERCLOSEWAIT1;
235 
236 			/* actively shutdown clcsock before peer close it,
237 			 * prevent peer from entering TIME_WAIT state.
238 			 */
239 			if (smc->clcsock && smc->clcsock->sk) {
240 				rc1 = kernel_sock_shutdown(smc->clcsock,
241 							   SHUT_RDWR);
242 				rc = rc ? rc : rc1;
243 			}
244 		} else {
245 			/* peer event has changed the state */
246 			goto again;
247 		}
248 		break;
249 	case SMC_APPFINCLOSEWAIT:
250 		/* socket already shutdown wr or both (active close) */
251 		if (txflags->peer_done_writing &&
252 		    !smc_close_sent_any_close(conn)) {
253 			/* just shutdown wr done, send close request */
254 			rc = smc_close_final(conn);
255 		}
256 		sk->sk_state = SMC_CLOSED;
257 		break;
258 	case SMC_APPCLOSEWAIT1:
259 	case SMC_APPCLOSEWAIT2:
260 		if (!smc_cdc_rxed_any_close(conn))
261 			smc_close_stream_wait(smc, timeout);
262 		release_sock(sk);
263 		cancel_delayed_work_sync(&conn->tx_work);
264 		lock_sock(sk);
265 		if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
266 		    sk->sk_state != SMC_APPCLOSEWAIT2)
267 			goto again;
268 		/* confirm close from peer */
269 		rc = smc_close_final(conn);
270 		if (smc_cdc_rxed_any_close(conn)) {
271 			/* peer has closed the socket already */
272 			sk->sk_state = SMC_CLOSED;
273 			sock_put(sk); /* postponed passive closing */
274 		} else {
275 			/* peer has just issued a shutdown write */
276 			sk->sk_state = SMC_PEERFINCLOSEWAIT;
277 		}
278 		break;
279 	case SMC_PEERCLOSEWAIT1:
280 	case SMC_PEERCLOSEWAIT2:
281 		if (txflags->peer_done_writing &&
282 		    !smc_close_sent_any_close(conn)) {
283 			/* just shutdown wr done, send close request */
284 			rc = smc_close_final(conn);
285 		}
286 		/* peer sending PeerConnectionClosed will cause transition */
287 		break;
288 	case SMC_PEERFINCLOSEWAIT:
289 		/* peer sending PeerConnectionClosed will cause transition */
290 		break;
291 	case SMC_PROCESSABORT:
292 		rc = smc_close_abort(conn);
293 		sk->sk_state = SMC_CLOSED;
294 		break;
295 	case SMC_PEERABORTWAIT:
296 		sk->sk_state = SMC_CLOSED;
297 		break;
298 	case SMC_CLOSED:
299 		/* nothing to do, add tracing in future patch */
300 		break;
301 	}
302 
303 	if (old_state != sk->sk_state)
304 		sk->sk_state_change(sk);
305 	return rc;
306 }
307 
308 static void smc_close_passive_abort_received(struct smc_sock *smc)
309 {
310 	struct smc_cdc_conn_state_flags *txflags =
311 		&smc->conn.local_tx_ctrl.conn_state_flags;
312 	struct sock *sk = &smc->sk;
313 
314 	switch (sk->sk_state) {
315 	case SMC_INIT:
316 	case SMC_ACTIVE:
317 	case SMC_APPCLOSEWAIT1:
318 		sk->sk_state = SMC_PROCESSABORT;
319 		sock_put(sk); /* passive closing */
320 		break;
321 	case SMC_APPFINCLOSEWAIT:
322 		sk->sk_state = SMC_PROCESSABORT;
323 		break;
324 	case SMC_PEERCLOSEWAIT1:
325 	case SMC_PEERCLOSEWAIT2:
326 		if (txflags->peer_done_writing &&
327 		    !smc_close_sent_any_close(&smc->conn))
328 			/* just shutdown, but not yet closed locally */
329 			sk->sk_state = SMC_PROCESSABORT;
330 		else
331 			sk->sk_state = SMC_CLOSED;
332 		sock_put(sk); /* passive closing */
333 		break;
334 	case SMC_APPCLOSEWAIT2:
335 	case SMC_PEERFINCLOSEWAIT:
336 		sk->sk_state = SMC_CLOSED;
337 		sock_put(sk); /* passive closing */
338 		break;
339 	case SMC_PEERABORTWAIT:
340 		sk->sk_state = SMC_CLOSED;
341 		break;
342 	case SMC_PROCESSABORT:
343 	/* nothing to do, add tracing in future patch */
344 		break;
345 	}
346 }
347 
348 /* Either some kind of closing has been received: peer_conn_closed,
349  * peer_conn_abort, or peer_done_writing
350  * or the link group of the connection terminates abnormally.
351  */
352 static void smc_close_passive_work(struct work_struct *work)
353 {
354 	struct smc_connection *conn = container_of(work,
355 						   struct smc_connection,
356 						   close_work);
357 	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
358 	struct smc_cdc_conn_state_flags *rxflags;
359 	bool release_clcsock = false;
360 	struct sock *sk = &smc->sk;
361 	int old_state;
362 
363 	lock_sock(sk);
364 	old_state = sk->sk_state;
365 
366 	rxflags = &conn->local_rx_ctrl.conn_state_flags;
367 	if (rxflags->peer_conn_abort) {
368 		/* peer has not received all data */
369 		smc_close_passive_abort_received(smc);
370 		release_sock(sk);
371 		cancel_delayed_work_sync(&conn->tx_work);
372 		lock_sock(sk);
373 		goto wakeup;
374 	}
375 
376 	switch (sk->sk_state) {
377 	case SMC_INIT:
378 		sk->sk_state = SMC_APPCLOSEWAIT1;
379 		break;
380 	case SMC_ACTIVE:
381 		sk->sk_state = SMC_APPCLOSEWAIT1;
382 		/* postpone sock_put() for passive closing to cover
383 		 * received SEND_SHUTDOWN as well
384 		 */
385 		break;
386 	case SMC_PEERCLOSEWAIT1:
387 		if (rxflags->peer_done_writing)
388 			sk->sk_state = SMC_PEERCLOSEWAIT2;
389 		fallthrough;
390 		/* to check for closing */
391 	case SMC_PEERCLOSEWAIT2:
392 		if (!smc_cdc_rxed_any_close(conn))
393 			break;
394 		if (sock_flag(sk, SOCK_DEAD) &&
395 		    smc_close_sent_any_close(conn)) {
396 			/* smc_release has already been called locally */
397 			sk->sk_state = SMC_CLOSED;
398 		} else {
399 			/* just shutdown, but not yet closed locally */
400 			sk->sk_state = SMC_APPFINCLOSEWAIT;
401 		}
402 		sock_put(sk); /* passive closing */
403 		break;
404 	case SMC_PEERFINCLOSEWAIT:
405 		if (smc_cdc_rxed_any_close(conn)) {
406 			sk->sk_state = SMC_CLOSED;
407 			sock_put(sk); /* passive closing */
408 		}
409 		break;
410 	case SMC_APPCLOSEWAIT1:
411 	case SMC_APPCLOSEWAIT2:
412 		/* postpone sock_put() for passive closing to cover
413 		 * received SEND_SHUTDOWN as well
414 		 */
415 		break;
416 	case SMC_APPFINCLOSEWAIT:
417 	case SMC_PEERABORTWAIT:
418 	case SMC_PROCESSABORT:
419 	case SMC_CLOSED:
420 		/* nothing to do, add tracing in future patch */
421 		break;
422 	}
423 
424 wakeup:
425 	sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
426 	sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
427 
428 	if (old_state != sk->sk_state) {
429 		sk->sk_state_change(sk);
430 		if ((sk->sk_state == SMC_CLOSED) &&
431 		    (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
432 			smc_conn_free(conn);
433 			if (smc->clcsock)
434 				release_clcsock = true;
435 		}
436 	}
437 	release_sock(sk);
438 	if (release_clcsock)
439 		smc_clcsock_release(smc);
440 	sock_put(sk); /* sock_hold done by schedulers of close_work */
441 }
442 
443 int smc_close_shutdown_write(struct smc_sock *smc)
444 {
445 	struct smc_connection *conn = &smc->conn;
446 	struct sock *sk = &smc->sk;
447 	int old_state;
448 	long timeout;
449 	int rc = 0;
450 
451 	timeout = current->flags & PF_EXITING ?
452 		  0 : sock_flag(sk, SOCK_LINGER) ?
453 		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
454 
455 	old_state = sk->sk_state;
456 again:
457 	switch (sk->sk_state) {
458 	case SMC_ACTIVE:
459 		smc_close_stream_wait(smc, timeout);
460 		release_sock(sk);
461 		cancel_delayed_work_sync(&conn->tx_work);
462 		lock_sock(sk);
463 		if (sk->sk_state != SMC_ACTIVE)
464 			goto again;
465 		/* send close wr request */
466 		rc = smc_close_wr(conn);
467 		sk->sk_state = SMC_PEERCLOSEWAIT1;
468 		break;
469 	case SMC_APPCLOSEWAIT1:
470 		/* passive close */
471 		if (!smc_cdc_rxed_any_close(conn))
472 			smc_close_stream_wait(smc, timeout);
473 		release_sock(sk);
474 		cancel_delayed_work_sync(&conn->tx_work);
475 		lock_sock(sk);
476 		if (sk->sk_state != SMC_APPCLOSEWAIT1)
477 			goto again;
478 		/* confirm close from peer */
479 		rc = smc_close_wr(conn);
480 		sk->sk_state = SMC_APPCLOSEWAIT2;
481 		break;
482 	case SMC_APPCLOSEWAIT2:
483 	case SMC_PEERFINCLOSEWAIT:
484 	case SMC_PEERCLOSEWAIT1:
485 	case SMC_PEERCLOSEWAIT2:
486 	case SMC_APPFINCLOSEWAIT:
487 	case SMC_PROCESSABORT:
488 	case SMC_PEERABORTWAIT:
489 		/* nothing to do, add tracing in future patch */
490 		break;
491 	}
492 
493 	if (old_state != sk->sk_state)
494 		sk->sk_state_change(sk);
495 	return rc;
496 }
497 
498 /* Initialize close properties on connection establishment. */
499 void smc_close_init(struct smc_sock *smc)
500 {
501 	INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
502 }
503