xref: /openbmc/linux/net/rxrpc/call_object.c (revision f36b5e44)
18c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling
28c3e34a4SDavid Howells  *
38c3e34a4SDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
48c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
58c3e34a4SDavid Howells  *
68c3e34a4SDavid Howells  * This program is free software; you can redistribute it and/or
78c3e34a4SDavid Howells  * modify it under the terms of the GNU General Public License
88c3e34a4SDavid Howells  * as published by the Free Software Foundation; either version
98c3e34a4SDavid Howells  * 2 of the License, or (at your option) any later version.
108c3e34a4SDavid Howells  */
118c3e34a4SDavid Howells 
128c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
138c3e34a4SDavid Howells 
148c3e34a4SDavid Howells #include <linux/slab.h>
158c3e34a4SDavid Howells #include <linux/module.h>
168c3e34a4SDavid Howells #include <linux/circ_buf.h>
178c3e34a4SDavid Howells #include <linux/spinlock_types.h>
188c3e34a4SDavid Howells #include <net/sock.h>
198c3e34a4SDavid Howells #include <net/af_rxrpc.h>
208c3e34a4SDavid Howells #include "ar-internal.h"
218c3e34a4SDavid Howells 
228c3e34a4SDavid Howells /*
238c3e34a4SDavid Howells  * Maximum lifetime of a call (in jiffies).
248c3e34a4SDavid Howells  */
258c3e34a4SDavid Howells unsigned int rxrpc_max_call_lifetime = 60 * HZ;
268c3e34a4SDavid Howells 
278c3e34a4SDavid Howells /*
288c3e34a4SDavid Howells  * Time till dead call expires after last use (in jiffies).
298c3e34a4SDavid Howells  */
308c3e34a4SDavid Howells unsigned int rxrpc_dead_call_expiry = 2 * HZ;
318c3e34a4SDavid Howells 
328c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
33999b69f8SDavid Howells 	[RXRPC_CALL_UNINITIALISED]		= "Uninit",
34999b69f8SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_CONN]		= "ClWtConn",
358c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_SEND_REQUEST]	= "ClSndReq",
368c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_REPLY]		= "ClAwtRpl",
378c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_RECV_REPLY]		= "ClRcvRpl",
388c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_FINAL_ACK]		= "ClFnlACK",
398c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SECURING]		= "SvSecure",
408c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACCEPTING]		= "SvAccept",
418c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_RECV_REQUEST]	= "SvRcvReq",
428c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACK_REQUEST]		= "SvAckReq",
438c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SEND_REPLY]		= "SvSndRpl",
448c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_AWAIT_ACK]		= "SvAwtACK",
458c3e34a4SDavid Howells 	[RXRPC_CALL_COMPLETE]			= "Complete",
468c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_BUSY]		= "SvBusy  ",
478c3e34a4SDavid Howells 	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
488c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
498c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
508c3e34a4SDavid Howells 	[RXRPC_CALL_DEAD]			= "Dead    ",
518c3e34a4SDavid Howells };
528c3e34a4SDavid Howells 
538c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
548c3e34a4SDavid Howells LIST_HEAD(rxrpc_calls);
558c3e34a4SDavid Howells DEFINE_RWLOCK(rxrpc_call_lock);
568c3e34a4SDavid Howells 
578c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work);
588c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call);
598c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call);
608c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call);
618c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call);
628c3e34a4SDavid Howells 
638c3e34a4SDavid Howells /*
648c3e34a4SDavid Howells  * find an extant server call
658c3e34a4SDavid Howells  * - called in process context with IRQs enabled
668c3e34a4SDavid Howells  */
678c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
688c3e34a4SDavid Howells 					      unsigned long user_call_ID)
698c3e34a4SDavid Howells {
708c3e34a4SDavid Howells 	struct rxrpc_call *call;
718c3e34a4SDavid Howells 	struct rb_node *p;
728c3e34a4SDavid Howells 
738c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
748c3e34a4SDavid Howells 
758c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
768c3e34a4SDavid Howells 
778c3e34a4SDavid Howells 	p = rx->calls.rb_node;
788c3e34a4SDavid Howells 	while (p) {
798c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
808c3e34a4SDavid Howells 
818c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
828c3e34a4SDavid Howells 			p = p->rb_left;
838c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
848c3e34a4SDavid Howells 			p = p->rb_right;
858c3e34a4SDavid Howells 		else
868c3e34a4SDavid Howells 			goto found_extant_call;
878c3e34a4SDavid Howells 	}
888c3e34a4SDavid Howells 
898c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
908c3e34a4SDavid Howells 	_leave(" = NULL");
918c3e34a4SDavid Howells 	return NULL;
928c3e34a4SDavid Howells 
938c3e34a4SDavid Howells found_extant_call:
948c3e34a4SDavid Howells 	rxrpc_get_call(call);
958c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
968c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
978c3e34a4SDavid Howells 	return call;
988c3e34a4SDavid Howells }
998c3e34a4SDavid Howells 
1008c3e34a4SDavid Howells /*
1018c3e34a4SDavid Howells  * allocate a new call
1028c3e34a4SDavid Howells  */
1038c3e34a4SDavid Howells static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
1048c3e34a4SDavid Howells {
1058c3e34a4SDavid Howells 	struct rxrpc_call *call;
1068c3e34a4SDavid Howells 
1078c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1088c3e34a4SDavid Howells 	if (!call)
1098c3e34a4SDavid Howells 		return NULL;
1108c3e34a4SDavid Howells 
1118c3e34a4SDavid Howells 	call->acks_winsz = 16;
1128c3e34a4SDavid Howells 	call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
1138c3e34a4SDavid Howells 				    gfp);
1148c3e34a4SDavid Howells 	if (!call->acks_window) {
1158c3e34a4SDavid Howells 		kmem_cache_free(rxrpc_call_jar, call);
1168c3e34a4SDavid Howells 		return NULL;
1178c3e34a4SDavid Howells 	}
1188c3e34a4SDavid Howells 
1198c3e34a4SDavid Howells 	setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
1208c3e34a4SDavid Howells 		    (unsigned long) call);
1218c3e34a4SDavid Howells 	setup_timer(&call->deadspan, &rxrpc_dead_call_expired,
1228c3e34a4SDavid Howells 		    (unsigned long) call);
1238c3e34a4SDavid Howells 	setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
1248c3e34a4SDavid Howells 		    (unsigned long) call);
1258c3e34a4SDavid Howells 	setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
1268c3e34a4SDavid Howells 		    (unsigned long) call);
1278c3e34a4SDavid Howells 	INIT_WORK(&call->destroyer, &rxrpc_destroy_call);
1288c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
129999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
1308c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
1318c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_queue);
1328c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_oos_queue);
1338c3e34a4SDavid Howells 	init_waitqueue_head(&call->tx_waitq);
1348c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
1358c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1368c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1378c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
1388c3e34a4SDavid Howells 
1398c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1408c3e34a4SDavid Howells 
1418c3e34a4SDavid Howells 	call->rx_data_expect = 1;
1428c3e34a4SDavid Howells 	call->rx_data_eaten = 0;
1438c3e34a4SDavid Howells 	call->rx_first_oos = 0;
1448c3e34a4SDavid Howells 	call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
1458c3e34a4SDavid Howells 	call->creation_jif = jiffies;
1468c3e34a4SDavid Howells 	return call;
1478c3e34a4SDavid Howells }
1488c3e34a4SDavid Howells 
1498c3e34a4SDavid Howells /*
150999b69f8SDavid Howells  * Allocate a new client call.
1518c3e34a4SDavid Howells  */
152aa390bbeSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
153999b69f8SDavid Howells 						  struct sockaddr_rxrpc *srx,
1548c3e34a4SDavid Howells 						  gfp_t gfp)
1558c3e34a4SDavid Howells {
1568c3e34a4SDavid Howells 	struct rxrpc_call *call;
1578c3e34a4SDavid Howells 
1588c3e34a4SDavid Howells 	_enter("");
1598c3e34a4SDavid Howells 
160999b69f8SDavid Howells 	ASSERT(rx->local != NULL);
1618c3e34a4SDavid Howells 
1628c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1638c3e34a4SDavid Howells 	if (!call)
1648c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
165999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
1668c3e34a4SDavid Howells 
1678c3e34a4SDavid Howells 	sock_hold(&rx->sk);
1688c3e34a4SDavid Howells 	call->socket = rx;
1698c3e34a4SDavid Howells 	call->rx_data_post = 1;
170999b69f8SDavid Howells 	call->service_id = srx->srx_service;
171999b69f8SDavid Howells 
172999b69f8SDavid Howells 	_leave(" = %p", call);
173999b69f8SDavid Howells 	return call;
174999b69f8SDavid Howells }
175999b69f8SDavid Howells 
176999b69f8SDavid Howells /*
177999b69f8SDavid Howells  * Begin client call.
178999b69f8SDavid Howells  */
179999b69f8SDavid Howells static int rxrpc_begin_client_call(struct rxrpc_call *call,
180999b69f8SDavid Howells 				   struct rxrpc_conn_parameters *cp,
181999b69f8SDavid Howells 				   struct sockaddr_rxrpc *srx,
182999b69f8SDavid Howells 				   gfp_t gfp)
183999b69f8SDavid Howells {
184999b69f8SDavid Howells 	int ret;
185999b69f8SDavid Howells 
186999b69f8SDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
187999b69f8SDavid Howells 	 * including channel number and call ID.
188999b69f8SDavid Howells 	 */
189aa390bbeSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
190999b69f8SDavid Howells 	if (ret < 0)
191999b69f8SDavid Howells 		return ret;
192999b69f8SDavid Howells 
193999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
194999b69f8SDavid Howells 
19585f32278SDavid Howells 	spin_lock(&call->conn->params.peer->lock);
19685f32278SDavid Howells 	hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
19785f32278SDavid Howells 	spin_unlock(&call->conn->params.peer->lock);
1988c3e34a4SDavid Howells 
1998c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
2008c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
201999b69f8SDavid Howells 	return 0;
2028c3e34a4SDavid Howells }
2038c3e34a4SDavid Howells 
2048c3e34a4SDavid Howells /*
2058c3e34a4SDavid Howells  * set up a call for the given data
2068c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2078c3e34a4SDavid Howells  */
2088c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
20919ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
210999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2118c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2128c3e34a4SDavid Howells 					 gfp_t gfp)
2138c3e34a4SDavid Howells {
2148c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2158c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
216999b69f8SDavid Howells 	int ret;
2178c3e34a4SDavid Howells 
218999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2198c3e34a4SDavid Howells 
220aa390bbeSDavid Howells 	call = rxrpc_alloc_client_call(rx, srx, gfp);
2218c3e34a4SDavid Howells 	if (IS_ERR(call)) {
2228c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2238c3e34a4SDavid Howells 		return call;
2248c3e34a4SDavid Howells 	}
2258c3e34a4SDavid Howells 
226999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2278c3e34a4SDavid Howells 	call->user_call_ID = user_call_ID;
2288c3e34a4SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
2298c3e34a4SDavid Howells 
2308c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2318c3e34a4SDavid Howells 
2328c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2338c3e34a4SDavid Howells 	parent = NULL;
2348c3e34a4SDavid Howells 	while (*pp) {
2358c3e34a4SDavid Howells 		parent = *pp;
2368c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2378c3e34a4SDavid Howells 
2388c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2398c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2408c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2418c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2428c3e34a4SDavid Howells 		else
2438c3e34a4SDavid Howells 			goto found_user_ID_now_present;
2448c3e34a4SDavid Howells 	}
2458c3e34a4SDavid Howells 
2468c3e34a4SDavid Howells 	rxrpc_get_call(call);
2478c3e34a4SDavid Howells 
2488c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2498c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
2508c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2518c3e34a4SDavid Howells 
2528c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
2538c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
2548c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
2558c3e34a4SDavid Howells 
256aa390bbeSDavid Howells 	ret = rxrpc_begin_client_call(call, cp, srx, gfp);
257999b69f8SDavid Howells 	if (ret < 0)
258999b69f8SDavid Howells 		goto error;
259999b69f8SDavid Howells 
2608c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2618c3e34a4SDavid Howells 
2628c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2638c3e34a4SDavid Howells 	return call;
2648c3e34a4SDavid Howells 
265999b69f8SDavid Howells error:
266999b69f8SDavid Howells 	write_lock(&rx->call_lock);
267999b69f8SDavid Howells 	rb_erase(&call->sock_node, &rx->calls);
268999b69f8SDavid Howells 	write_unlock(&rx->call_lock);
269999b69f8SDavid Howells 	rxrpc_put_call(call);
270999b69f8SDavid Howells 
271999b69f8SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
272d1e858c5SDavid Howells 	list_del_init(&call->link);
273999b69f8SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
274999b69f8SDavid Howells 
27517b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
276d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
277999b69f8SDavid Howells 	rxrpc_put_call(call);
278999b69f8SDavid Howells 	_leave(" = %d", ret);
279999b69f8SDavid Howells 	return ERR_PTR(ret);
280999b69f8SDavid Howells 
2818c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2828c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2838c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2848c3e34a4SDavid Howells 	 * same time in different threads.
2858c3e34a4SDavid Howells 	 */
2868c3e34a4SDavid Howells found_user_ID_now_present:
2878c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
28817b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
289d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
2908c3e34a4SDavid Howells 	rxrpc_put_call(call);
2918c3e34a4SDavid Howells 	_leave(" = -EEXIST [%p]", call);
2928c3e34a4SDavid Howells 	return ERR_PTR(-EEXIST);
2938c3e34a4SDavid Howells }
2948c3e34a4SDavid Howells 
2958c3e34a4SDavid Howells /*
2968c3e34a4SDavid Howells  * set up an incoming call
2978c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2988c3e34a4SDavid Howells  */
2998c3e34a4SDavid Howells struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
3008c3e34a4SDavid Howells 				       struct rxrpc_connection *conn,
30142886ffeSDavid Howells 				       struct sk_buff *skb)
3028c3e34a4SDavid Howells {
30342886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3048c3e34a4SDavid Howells 	struct rxrpc_call *call, *candidate;
305a1399f8bSDavid Howells 	u32 call_id, chan;
3068c3e34a4SDavid Howells 
3078c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
3088c3e34a4SDavid Howells 
3098c3e34a4SDavid Howells 	ASSERT(rx != NULL);
3108c3e34a4SDavid Howells 
3118c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
3128c3e34a4SDavid Howells 	if (!candidate)
3138c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
3148c3e34a4SDavid Howells 
315a1399f8bSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
3168c3e34a4SDavid Howells 	candidate->socket	= rx;
3178c3e34a4SDavid Howells 	candidate->conn		= conn;
31842886ffeSDavid Howells 	candidate->cid		= sp->hdr.cid;
31942886ffeSDavid Howells 	candidate->call_id	= sp->hdr.callNumber;
320a1399f8bSDavid Howells 	candidate->channel	= chan;
3218c3e34a4SDavid Howells 	candidate->rx_data_post	= 0;
3228c3e34a4SDavid Howells 	candidate->state	= RXRPC_CALL_SERVER_ACCEPTING;
323dabe5a79SDavid Howells 	candidate->flags	|= (1 << RXRPC_CALL_IS_SERVICE);
3248c3e34a4SDavid Howells 	if (conn->security_ix > 0)
3258c3e34a4SDavid Howells 		candidate->state = RXRPC_CALL_SERVER_SECURING;
3268c3e34a4SDavid Howells 
327a1399f8bSDavid Howells 	spin_lock(&conn->channel_lock);
3288c3e34a4SDavid Howells 
3298c3e34a4SDavid Howells 	/* set the channel for this call */
330a1399f8bSDavid Howells 	call = rcu_dereference_protected(conn->channels[chan].call,
331a1399f8bSDavid Howells 					 lockdep_is_held(&conn->channel_lock));
332a1399f8bSDavid Howells 
3338c3e34a4SDavid Howells 	_debug("channel[%u] is %p", candidate->channel, call);
33442886ffeSDavid Howells 	if (call && call->call_id == sp->hdr.callNumber) {
3358c3e34a4SDavid Howells 		/* already set; must've been a duplicate packet */
3368c3e34a4SDavid Howells 		_debug("extant call [%d]", call->state);
3378c3e34a4SDavid Howells 		ASSERTCMP(call->conn, ==, conn);
3388c3e34a4SDavid Howells 
3398c3e34a4SDavid Howells 		read_lock(&call->state_lock);
3408c3e34a4SDavid Howells 		switch (call->state) {
3418c3e34a4SDavid Howells 		case RXRPC_CALL_LOCALLY_ABORTED:
3428c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
3438c3e34a4SDavid Howells 				rxrpc_queue_call(call);
3448c3e34a4SDavid Howells 		case RXRPC_CALL_REMOTELY_ABORTED:
3458c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3468c3e34a4SDavid Howells 			goto aborted_call;
3478c3e34a4SDavid Howells 		default:
3488c3e34a4SDavid Howells 			rxrpc_get_call(call);
3498c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3508c3e34a4SDavid Howells 			goto extant_call;
3518c3e34a4SDavid Howells 		}
3528c3e34a4SDavid Howells 	}
3538c3e34a4SDavid Howells 
3548c3e34a4SDavid Howells 	if (call) {
3558c3e34a4SDavid Howells 		/* it seems the channel is still in use from the previous call
3568c3e34a4SDavid Howells 		 * - ditch the old binding if its call is now complete */
3578c3e34a4SDavid Howells 		_debug("CALL: %u { %s }",
3588c3e34a4SDavid Howells 		       call->debug_id, rxrpc_call_states[call->state]);
3598c3e34a4SDavid Howells 
3608c3e34a4SDavid Howells 		if (call->state >= RXRPC_CALL_COMPLETE) {
361a1399f8bSDavid Howells 			__rxrpc_disconnect_call(call);
3628c3e34a4SDavid Howells 		} else {
363a1399f8bSDavid Howells 			spin_unlock(&conn->channel_lock);
3648c3e34a4SDavid Howells 			kmem_cache_free(rxrpc_call_jar, candidate);
3658c3e34a4SDavid Howells 			_leave(" = -EBUSY");
3668c3e34a4SDavid Howells 			return ERR_PTR(-EBUSY);
3678c3e34a4SDavid Howells 		}
3688c3e34a4SDavid Howells 	}
3698c3e34a4SDavid Howells 
3708c3e34a4SDavid Howells 	/* check the call number isn't duplicate */
3718c3e34a4SDavid Howells 	_debug("check dup");
37242886ffeSDavid Howells 	call_id = sp->hdr.callNumber;
3738c3e34a4SDavid Howells 
374a1399f8bSDavid Howells 	/* We just ignore calls prior to the current call ID.  Terminated calls
375a1399f8bSDavid Howells 	 * are handled via the connection.
3768c3e34a4SDavid Howells 	 */
377a1399f8bSDavid Howells 	if (call_id <= conn->channels[chan].call_counter)
378a1399f8bSDavid Howells 		goto old_call; /* TODO: Just drop packet */
3798c3e34a4SDavid Howells 
3808c3e34a4SDavid Howells 	/* make the call available */
3818c3e34a4SDavid Howells 	_debug("new call");
3828c3e34a4SDavid Howells 	call = candidate;
3838c3e34a4SDavid Howells 	candidate = NULL;
384a1399f8bSDavid Howells 	conn->channels[chan].call_counter = call_id;
385a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3868c3e34a4SDavid Howells 	sock_hold(&rx->sk);
3875627cc8bSDavid Howells 	rxrpc_get_connection(conn);
388a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
3898c3e34a4SDavid Howells 
39085f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
39185f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
39285f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3938c3e34a4SDavid Howells 
3948c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
3958c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
3968c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
3978c3e34a4SDavid Howells 
39819ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
3998c3e34a4SDavid Howells 
4008c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
4018c3e34a4SDavid Howells 
4028c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
4038c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
4048c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
4058c3e34a4SDavid Howells 	return call;
4068c3e34a4SDavid Howells 
4078c3e34a4SDavid Howells extant_call:
408a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4098c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4108c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
4118c3e34a4SDavid Howells 	return call;
4128c3e34a4SDavid Howells 
4138c3e34a4SDavid Howells aborted_call:
414a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4158c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4168c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
4178c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
4188c3e34a4SDavid Howells 
4198c3e34a4SDavid Howells old_call:
420a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4218c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4228c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
4238c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
4248c3e34a4SDavid Howells }
4258c3e34a4SDavid Howells 
4268c3e34a4SDavid Howells /*
4278c3e34a4SDavid Howells  * detach a call from a socket and set up for release
4288c3e34a4SDavid Howells  */
4298c3e34a4SDavid Howells void rxrpc_release_call(struct rxrpc_call *call)
4308c3e34a4SDavid Howells {
4318c3e34a4SDavid Howells 	struct rxrpc_connection *conn = call->conn;
4328c3e34a4SDavid Howells 	struct rxrpc_sock *rx = call->socket;
4338c3e34a4SDavid Howells 
4348c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
4358c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
4368c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
4378c3e34a4SDavid Howells 	       call->rx_first_oos);
4388c3e34a4SDavid Howells 
4398c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4408c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4418c3e34a4SDavid Howells 		BUG();
4428c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4438c3e34a4SDavid Howells 
4448c3e34a4SDavid Howells 	/* dissociate from the socket
4458c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
4468c3e34a4SDavid Howells 	 */
4478c3e34a4SDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4488c3e34a4SDavid Howells 
449e653cfe4SDavid Howells 	spin_lock(&conn->params.peer->lock);
450e653cfe4SDavid Howells 	hlist_del_init(&call->error_link);
451e653cfe4SDavid Howells 	spin_unlock(&conn->params.peer->lock);
452e653cfe4SDavid Howells 
4538c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
4548c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
4558c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4568c3e34a4SDavid Howells 		       call, call->events, call->flags);
4578c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
4588c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
4598c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
4608c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4618c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4628c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4638c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
4648c3e34a4SDavid Howells 	}
4658c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
4668c3e34a4SDavid Howells 
4678c3e34a4SDavid Howells 	/* free up the channel for reuse */
468a1399f8bSDavid Howells 	write_lock_bh(&call->state_lock);
4698c3e34a4SDavid Howells 
4708c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
4718c3e34a4SDavid Howells 	    call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
4728c3e34a4SDavid Howells 		_debug("+++ ABORTING STATE %d +++\n", call->state);
4738c3e34a4SDavid Howells 		call->state = RXRPC_CALL_LOCALLY_ABORTED;
4748c3e34a4SDavid Howells 		call->local_abort = RX_CALL_DEAD;
4758c3e34a4SDavid Howells 	}
476a1399f8bSDavid Howells 	write_unlock_bh(&call->state_lock);
4778c3e34a4SDavid Howells 
478e653cfe4SDavid Howells 	rxrpc_disconnect_call(call);
479e653cfe4SDavid Howells 
4808c3e34a4SDavid Howells 	/* clean up the Rx queue */
4818c3e34a4SDavid Howells 	if (!skb_queue_empty(&call->rx_queue) ||
4828c3e34a4SDavid Howells 	    !skb_queue_empty(&call->rx_oos_queue)) {
4838c3e34a4SDavid Howells 		struct rxrpc_skb_priv *sp;
4848c3e34a4SDavid Howells 		struct sk_buff *skb;
4858c3e34a4SDavid Howells 
4868c3e34a4SDavid Howells 		_debug("purge Rx queues");
4878c3e34a4SDavid Howells 
4888c3e34a4SDavid Howells 		spin_lock_bh(&call->lock);
4898c3e34a4SDavid Howells 		while ((skb = skb_dequeue(&call->rx_queue)) ||
4908c3e34a4SDavid Howells 		       (skb = skb_dequeue(&call->rx_oos_queue))) {
4918c3e34a4SDavid Howells 			spin_unlock_bh(&call->lock);
4928c3e34a4SDavid Howells 
49355cae7a4SArnd Bergmann 			sp = rxrpc_skb(skb);
4948c3e34a4SDavid Howells 			_debug("- zap %s %%%u #%u",
4958c3e34a4SDavid Howells 			       rxrpc_pkts[sp->hdr.type],
4968c3e34a4SDavid Howells 			       sp->hdr.serial, sp->hdr.seq);
4978c3e34a4SDavid Howells 			rxrpc_free_skb(skb);
4988c3e34a4SDavid Howells 			spin_lock_bh(&call->lock);
4998c3e34a4SDavid Howells 		}
5008c3e34a4SDavid Howells 		spin_unlock_bh(&call->lock);
5018c3e34a4SDavid Howells 
5028c3e34a4SDavid Howells 		ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
5038c3e34a4SDavid Howells 	}
5048c3e34a4SDavid Howells 
5058c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
5068c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
5078c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
5088c3e34a4SDavid Howells 	call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
5098c3e34a4SDavid Howells 	add_timer(&call->deadspan);
5108c3e34a4SDavid Howells 
5118c3e34a4SDavid Howells 	_leave("");
5128c3e34a4SDavid Howells }
5138c3e34a4SDavid Howells 
5148c3e34a4SDavid Howells /*
5158c3e34a4SDavid Howells  * handle a dead call being ready for reaping
5168c3e34a4SDavid Howells  */
5178c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call)
5188c3e34a4SDavid Howells {
5198c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
5208c3e34a4SDavid Howells 
5218c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
5228c3e34a4SDavid Howells 
5238c3e34a4SDavid Howells 	write_lock_bh(&call->state_lock);
5248c3e34a4SDavid Howells 	call->state = RXRPC_CALL_DEAD;
5258c3e34a4SDavid Howells 	write_unlock_bh(&call->state_lock);
5268c3e34a4SDavid Howells 	rxrpc_put_call(call);
5278c3e34a4SDavid Howells }
5288c3e34a4SDavid Howells 
5298c3e34a4SDavid Howells /*
5308c3e34a4SDavid Howells  * mark a call as to be released, aborting it if it's still in progress
5318c3e34a4SDavid Howells  * - called with softirqs disabled
5328c3e34a4SDavid Howells  */
5338c3e34a4SDavid Howells static void rxrpc_mark_call_released(struct rxrpc_call *call)
5348c3e34a4SDavid Howells {
5358c3e34a4SDavid Howells 	bool sched;
5368c3e34a4SDavid Howells 
5378c3e34a4SDavid Howells 	write_lock(&call->state_lock);
5388c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_DEAD) {
5398c3e34a4SDavid Howells 		sched = false;
5408c3e34a4SDavid Howells 		if (call->state < RXRPC_CALL_COMPLETE) {
5418c3e34a4SDavid Howells 			_debug("abort call %p", call);
5428c3e34a4SDavid Howells 			call->state = RXRPC_CALL_LOCALLY_ABORTED;
5438c3e34a4SDavid Howells 			call->local_abort = RX_CALL_DEAD;
5448c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
5458c3e34a4SDavid Howells 				sched = true;
5468c3e34a4SDavid Howells 		}
5478c3e34a4SDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
5488c3e34a4SDavid Howells 			sched = true;
5498c3e34a4SDavid Howells 		if (sched)
5508c3e34a4SDavid Howells 			rxrpc_queue_call(call);
5518c3e34a4SDavid Howells 	}
5528c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
5538c3e34a4SDavid Howells }
5548c3e34a4SDavid Howells 
5558c3e34a4SDavid Howells /*
5568c3e34a4SDavid Howells  * release all the calls associated with a socket
5578c3e34a4SDavid Howells  */
5588c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
5598c3e34a4SDavid Howells {
5608c3e34a4SDavid Howells 	struct rxrpc_call *call;
5618c3e34a4SDavid Howells 	struct rb_node *p;
5628c3e34a4SDavid Howells 
5638c3e34a4SDavid Howells 	_enter("%p", rx);
5648c3e34a4SDavid Howells 
5658c3e34a4SDavid Howells 	read_lock_bh(&rx->call_lock);
5668c3e34a4SDavid Howells 
5678c3e34a4SDavid Howells 	/* kill the not-yet-accepted incoming calls */
5688c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->secureq, accept_link) {
5698c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
5708c3e34a4SDavid Howells 	}
5718c3e34a4SDavid Howells 
5728c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->acceptq, accept_link) {
5738c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
5748c3e34a4SDavid Howells 	}
5758c3e34a4SDavid Howells 
576f36b5e44SDavid Howells 	/* mark all the calls as no longer wanting incoming packets */
577f36b5e44SDavid Howells 	for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
578f36b5e44SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
579f36b5e44SDavid Howells 		rxrpc_mark_call_released(call);
580f36b5e44SDavid Howells 	}
581f36b5e44SDavid Howells 
5828c3e34a4SDavid Howells 	read_unlock_bh(&rx->call_lock);
5838c3e34a4SDavid Howells 	_leave("");
5848c3e34a4SDavid Howells }
5858c3e34a4SDavid Howells 
5868c3e34a4SDavid Howells /*
5878c3e34a4SDavid Howells  * release a call
5888c3e34a4SDavid Howells  */
5898c3e34a4SDavid Howells void __rxrpc_put_call(struct rxrpc_call *call)
5908c3e34a4SDavid Howells {
5918c3e34a4SDavid Howells 	ASSERT(call != NULL);
5928c3e34a4SDavid Howells 
5938c3e34a4SDavid Howells 	_enter("%p{u=%d}", call, atomic_read(&call->usage));
5948c3e34a4SDavid Howells 
5958c3e34a4SDavid Howells 	ASSERTCMP(atomic_read(&call->usage), >, 0);
5968c3e34a4SDavid Howells 
5978c3e34a4SDavid Howells 	if (atomic_dec_and_test(&call->usage)) {
5988c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
599372ee163SDavid Howells 		WARN_ON(atomic_read(&call->skb_count) != 0);
6008c3e34a4SDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
6018c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
6028c3e34a4SDavid Howells 	}
6038c3e34a4SDavid Howells 	_leave("");
6048c3e34a4SDavid Howells }
6058c3e34a4SDavid Howells 
6068c3e34a4SDavid Howells /*
607dee46364SDavid Howells  * Final call destruction under RCU.
608dee46364SDavid Howells  */
609dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
610dee46364SDavid Howells {
611dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
612dee46364SDavid Howells 
613dee46364SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
614dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
615dee46364SDavid Howells }
616dee46364SDavid Howells 
617dee46364SDavid Howells /*
6188c3e34a4SDavid Howells  * clean up a call
6198c3e34a4SDavid Howells  */
6208c3e34a4SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call)
6218c3e34a4SDavid Howells {
6228c3e34a4SDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
6238c3e34a4SDavid Howells 
6248c3e34a4SDavid Howells 	ASSERT(call->socket);
6258c3e34a4SDavid Howells 
6268c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
6278c3e34a4SDavid Howells 
6288c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
6298c3e34a4SDavid Howells 	del_timer_sync(&call->deadspan);
6308c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
6318c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
6328c3e34a4SDavid Howells 
6338c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
6348c3e34a4SDavid Howells 	ASSERTCMP(call->events, ==, 0);
6358c3e34a4SDavid Howells 	if (work_pending(&call->processor)) {
6368c3e34a4SDavid Howells 		_debug("defer destroy");
6378c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
6388c3e34a4SDavid Howells 		return;
6398c3e34a4SDavid Howells 	}
6408c3e34a4SDavid Howells 
641e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
6428c3e34a4SDavid Howells 
6438c3e34a4SDavid Howells 	if (call->acks_window) {
6448c3e34a4SDavid Howells 		_debug("kill Tx window %d",
6458c3e34a4SDavid Howells 		       CIRC_CNT(call->acks_head, call->acks_tail,
6468c3e34a4SDavid Howells 				call->acks_winsz));
6478c3e34a4SDavid Howells 		smp_mb();
6488c3e34a4SDavid Howells 		while (CIRC_CNT(call->acks_head, call->acks_tail,
6498c3e34a4SDavid Howells 				call->acks_winsz) > 0) {
6508c3e34a4SDavid Howells 			struct rxrpc_skb_priv *sp;
6518c3e34a4SDavid Howells 			unsigned long _skb;
6528c3e34a4SDavid Howells 
6538c3e34a4SDavid Howells 			_skb = call->acks_window[call->acks_tail] & ~1;
6548c3e34a4SDavid Howells 			sp = rxrpc_skb((struct sk_buff *)_skb);
6558c3e34a4SDavid Howells 			_debug("+++ clear Tx %u", sp->hdr.seq);
6568c3e34a4SDavid Howells 			rxrpc_free_skb((struct sk_buff *)_skb);
6578c3e34a4SDavid Howells 			call->acks_tail =
6588c3e34a4SDavid Howells 				(call->acks_tail + 1) & (call->acks_winsz - 1);
6598c3e34a4SDavid Howells 		}
6608c3e34a4SDavid Howells 
6618c3e34a4SDavid Howells 		kfree(call->acks_window);
6628c3e34a4SDavid Howells 	}
6638c3e34a4SDavid Howells 
6648c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
6658c3e34a4SDavid Howells 
6668c3e34a4SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
6678c3e34a4SDavid Howells 	ASSERT(skb_queue_empty(&call->rx_oos_queue));
6688c3e34a4SDavid Howells 	sock_put(&call->socket->sk);
669dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
6708c3e34a4SDavid Howells }
6718c3e34a4SDavid Howells 
6728c3e34a4SDavid Howells /*
6738c3e34a4SDavid Howells  * destroy a call
6748c3e34a4SDavid Howells  */
6758c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work)
6768c3e34a4SDavid Howells {
6778c3e34a4SDavid Howells 	struct rxrpc_call *call =
6788c3e34a4SDavid Howells 		container_of(work, struct rxrpc_call, destroyer);
6798c3e34a4SDavid Howells 
6808c3e34a4SDavid Howells 	_enter("%p{%d,%d,%p}",
6818c3e34a4SDavid Howells 	       call, atomic_read(&call->usage), call->channel, call->conn);
6828c3e34a4SDavid Howells 
6838c3e34a4SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
6848c3e34a4SDavid Howells 
6858c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
6868c3e34a4SDavid Howells 	list_del_init(&call->link);
6878c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
6888c3e34a4SDavid Howells 
6898c3e34a4SDavid Howells 	rxrpc_cleanup_call(call);
6908c3e34a4SDavid Howells 	_leave("");
6918c3e34a4SDavid Howells }
6928c3e34a4SDavid Howells 
6938c3e34a4SDavid Howells /*
6948c3e34a4SDavid Howells  * preemptively destroy all the call records from a transport endpoint rather
6958c3e34a4SDavid Howells  * than waiting for them to time out
6968c3e34a4SDavid Howells  */
6978c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
6988c3e34a4SDavid Howells {
6998c3e34a4SDavid Howells 	struct rxrpc_call *call;
7008c3e34a4SDavid Howells 
7018c3e34a4SDavid Howells 	_enter("");
7028c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
7038c3e34a4SDavid Howells 
7048c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
7058c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
7068c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
7078c3e34a4SDavid Howells 
7088c3e34a4SDavid Howells 		list_del_init(&call->link);
7098c3e34a4SDavid Howells 
7108c3e34a4SDavid Howells 		switch (atomic_read(&call->usage)) {
7118c3e34a4SDavid Howells 		case 0:
7128c3e34a4SDavid Howells 			ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
7138c3e34a4SDavid Howells 			break;
7148c3e34a4SDavid Howells 		case 1:
7158c3e34a4SDavid Howells 			if (del_timer_sync(&call->deadspan) != 0 &&
7168c3e34a4SDavid Howells 			    call->state != RXRPC_CALL_DEAD)
7178c3e34a4SDavid Howells 				rxrpc_dead_call_expired((unsigned long) call);
7188c3e34a4SDavid Howells 			if (call->state != RXRPC_CALL_DEAD)
7198c3e34a4SDavid Howells 				break;
7208c3e34a4SDavid Howells 		default:
7218c3e34a4SDavid Howells 			pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
7228c3e34a4SDavid Howells 			       call, atomic_read(&call->usage),
7238c3e34a4SDavid Howells 			       atomic_read(&call->ackr_not_idle),
7248c3e34a4SDavid Howells 			       rxrpc_call_states[call->state],
7258c3e34a4SDavid Howells 			       call->flags, call->events);
7268c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_queue))
7278c3e34a4SDavid Howells 				pr_err("Rx queue occupied\n");
7288c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_oos_queue))
7298c3e34a4SDavid Howells 				pr_err("OOS queue occupied\n");
7308c3e34a4SDavid Howells 			break;
7318c3e34a4SDavid Howells 		}
7328c3e34a4SDavid Howells 
7338c3e34a4SDavid Howells 		write_unlock_bh(&rxrpc_call_lock);
7348c3e34a4SDavid Howells 		cond_resched();
7358c3e34a4SDavid Howells 		write_lock_bh(&rxrpc_call_lock);
7368c3e34a4SDavid Howells 	}
7378c3e34a4SDavid Howells 
7388c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
7398c3e34a4SDavid Howells 	_leave("");
7408c3e34a4SDavid Howells }
7418c3e34a4SDavid Howells 
7428c3e34a4SDavid Howells /*
7438c3e34a4SDavid Howells  * handle call lifetime being exceeded
7448c3e34a4SDavid Howells  */
7458c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call)
7468c3e34a4SDavid Howells {
7478c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7488c3e34a4SDavid Howells 
7498c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7508c3e34a4SDavid Howells 		return;
7518c3e34a4SDavid Howells 
7528c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7538c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
7548c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
7558c3e34a4SDavid Howells 		set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
7568c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7578c3e34a4SDavid Howells 	}
7588c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
7598c3e34a4SDavid Howells }
7608c3e34a4SDavid Howells 
7618c3e34a4SDavid Howells /*
7628c3e34a4SDavid Howells  * handle resend timer expiry
7638c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
7648c3e34a4SDavid Howells  */
7658c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
7668c3e34a4SDavid Howells {
7678c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7688c3e34a4SDavid Howells 
7698c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7708c3e34a4SDavid Howells 
7718c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7728c3e34a4SDavid Howells 		return;
7738c3e34a4SDavid Howells 
7748c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
7758c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
7768c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7778c3e34a4SDavid Howells }
7788c3e34a4SDavid Howells 
7798c3e34a4SDavid Howells /*
7808c3e34a4SDavid Howells  * handle ACK timer expiry
7818c3e34a4SDavid Howells  */
7828c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
7838c3e34a4SDavid Howells {
7848c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7858c3e34a4SDavid Howells 
7868c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7878c3e34a4SDavid Howells 
7888c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7898c3e34a4SDavid Howells 		return;
7908c3e34a4SDavid Howells 
7918c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
7928c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
7938c3e34a4SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
7948c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7958c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
7968c3e34a4SDavid Howells }
797