xref: /openbmc/linux/net/rxrpc/call_object.c (revision f5c17aae)
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",
46f5c17aaeSDavid Howells 	[RXRPC_CALL_DEAD]			= "Dead    ",
47f5c17aaeSDavid Howells };
48f5c17aaeSDavid Howells 
49f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
50f5c17aaeSDavid Howells 	[RXRPC_CALL_SUCCEEDED]			= "Complete",
518c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_BUSY]		= "SvBusy  ",
528c3e34a4SDavid Howells 	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
538c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
54f5c17aaeSDavid Howells 	[RXRPC_CALL_LOCAL_ERROR]		= "LocError",
558c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
568c3e34a4SDavid Howells };
578c3e34a4SDavid Howells 
588c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
598c3e34a4SDavid Howells LIST_HEAD(rxrpc_calls);
608c3e34a4SDavid Howells DEFINE_RWLOCK(rxrpc_call_lock);
618c3e34a4SDavid Howells 
628c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work);
638c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call);
648c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call);
658c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call);
668c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call);
678c3e34a4SDavid Howells 
688c3e34a4SDavid Howells /*
698c3e34a4SDavid Howells  * find an extant server call
708c3e34a4SDavid Howells  * - called in process context with IRQs enabled
718c3e34a4SDavid Howells  */
728c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
738c3e34a4SDavid Howells 					      unsigned long user_call_ID)
748c3e34a4SDavid Howells {
758c3e34a4SDavid Howells 	struct rxrpc_call *call;
768c3e34a4SDavid Howells 	struct rb_node *p;
778c3e34a4SDavid Howells 
788c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
798c3e34a4SDavid Howells 
808c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
818c3e34a4SDavid Howells 
828c3e34a4SDavid Howells 	p = rx->calls.rb_node;
838c3e34a4SDavid Howells 	while (p) {
848c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
858c3e34a4SDavid Howells 
868c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
878c3e34a4SDavid Howells 			p = p->rb_left;
888c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
898c3e34a4SDavid Howells 			p = p->rb_right;
908c3e34a4SDavid Howells 		else
918c3e34a4SDavid Howells 			goto found_extant_call;
928c3e34a4SDavid Howells 	}
938c3e34a4SDavid Howells 
948c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
958c3e34a4SDavid Howells 	_leave(" = NULL");
968c3e34a4SDavid Howells 	return NULL;
978c3e34a4SDavid Howells 
988c3e34a4SDavid Howells found_extant_call:
998c3e34a4SDavid Howells 	rxrpc_get_call(call);
1008c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1018c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
1028c3e34a4SDavid Howells 	return call;
1038c3e34a4SDavid Howells }
1048c3e34a4SDavid Howells 
1058c3e34a4SDavid Howells /*
1068c3e34a4SDavid Howells  * allocate a new call
1078c3e34a4SDavid Howells  */
1088c3e34a4SDavid Howells static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
1098c3e34a4SDavid Howells {
1108c3e34a4SDavid Howells 	struct rxrpc_call *call;
1118c3e34a4SDavid Howells 
1128c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1138c3e34a4SDavid Howells 	if (!call)
1148c3e34a4SDavid Howells 		return NULL;
1158c3e34a4SDavid Howells 
1168c3e34a4SDavid Howells 	call->acks_winsz = 16;
1178c3e34a4SDavid Howells 	call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
1188c3e34a4SDavid Howells 				    gfp);
1198c3e34a4SDavid Howells 	if (!call->acks_window) {
1208c3e34a4SDavid Howells 		kmem_cache_free(rxrpc_call_jar, call);
1218c3e34a4SDavid Howells 		return NULL;
1228c3e34a4SDavid Howells 	}
1238c3e34a4SDavid Howells 
1248c3e34a4SDavid Howells 	setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
1258c3e34a4SDavid Howells 		    (unsigned long) call);
1268c3e34a4SDavid Howells 	setup_timer(&call->deadspan, &rxrpc_dead_call_expired,
1278c3e34a4SDavid Howells 		    (unsigned long) call);
1288c3e34a4SDavid Howells 	setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
1298c3e34a4SDavid Howells 		    (unsigned long) call);
1308c3e34a4SDavid Howells 	setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
1318c3e34a4SDavid Howells 		    (unsigned long) call);
1328c3e34a4SDavid Howells 	INIT_WORK(&call->destroyer, &rxrpc_destroy_call);
1338c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
134999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
13545025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1368c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
1378c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_queue);
1388c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_oos_queue);
13945025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1408c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
1418c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1428c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1438c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
1448c3e34a4SDavid Howells 
1458c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1468c3e34a4SDavid Howells 
1478c3e34a4SDavid Howells 	call->rx_data_expect = 1;
1488c3e34a4SDavid Howells 	call->rx_data_eaten = 0;
1498c3e34a4SDavid Howells 	call->rx_first_oos = 0;
1508c3e34a4SDavid Howells 	call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
1518c3e34a4SDavid Howells 	call->creation_jif = jiffies;
1528c3e34a4SDavid Howells 	return call;
1538c3e34a4SDavid Howells }
1548c3e34a4SDavid Howells 
1558c3e34a4SDavid Howells /*
156999b69f8SDavid Howells  * Allocate a new client call.
1578c3e34a4SDavid Howells  */
158aa390bbeSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
159999b69f8SDavid Howells 						  struct sockaddr_rxrpc *srx,
1608c3e34a4SDavid Howells 						  gfp_t gfp)
1618c3e34a4SDavid Howells {
1628c3e34a4SDavid Howells 	struct rxrpc_call *call;
1638c3e34a4SDavid Howells 
1648c3e34a4SDavid Howells 	_enter("");
1658c3e34a4SDavid Howells 
166999b69f8SDavid Howells 	ASSERT(rx->local != NULL);
1678c3e34a4SDavid Howells 
1688c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1698c3e34a4SDavid Howells 	if (!call)
1708c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
171999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
1728c3e34a4SDavid Howells 
1738c3e34a4SDavid Howells 	sock_hold(&rx->sk);
1748c3e34a4SDavid Howells 	call->socket = rx;
1758c3e34a4SDavid Howells 	call->rx_data_post = 1;
176999b69f8SDavid Howells 	call->service_id = srx->srx_service;
177999b69f8SDavid Howells 
178999b69f8SDavid Howells 	_leave(" = %p", call);
179999b69f8SDavid Howells 	return call;
180999b69f8SDavid Howells }
181999b69f8SDavid Howells 
182999b69f8SDavid Howells /*
183999b69f8SDavid Howells  * Begin client call.
184999b69f8SDavid Howells  */
185999b69f8SDavid Howells static int rxrpc_begin_client_call(struct rxrpc_call *call,
186999b69f8SDavid Howells 				   struct rxrpc_conn_parameters *cp,
187999b69f8SDavid Howells 				   struct sockaddr_rxrpc *srx,
188999b69f8SDavid Howells 				   gfp_t gfp)
189999b69f8SDavid Howells {
190999b69f8SDavid Howells 	int ret;
191999b69f8SDavid Howells 
192999b69f8SDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
193999b69f8SDavid Howells 	 * including channel number and call ID.
194999b69f8SDavid Howells 	 */
195aa390bbeSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
196999b69f8SDavid Howells 	if (ret < 0)
197999b69f8SDavid Howells 		return ret;
198999b69f8SDavid Howells 
199999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
200999b69f8SDavid Howells 
20185f32278SDavid Howells 	spin_lock(&call->conn->params.peer->lock);
20285f32278SDavid Howells 	hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
20385f32278SDavid Howells 	spin_unlock(&call->conn->params.peer->lock);
2048c3e34a4SDavid Howells 
2058c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
2068c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
207999b69f8SDavid Howells 	return 0;
2088c3e34a4SDavid Howells }
2098c3e34a4SDavid Howells 
2108c3e34a4SDavid Howells /*
2118c3e34a4SDavid Howells  * set up a call for the given data
2128c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2138c3e34a4SDavid Howells  */
2148c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
21519ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
216999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2178c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2188c3e34a4SDavid Howells 					 gfp_t gfp)
2198c3e34a4SDavid Howells {
2208c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2218c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
222999b69f8SDavid Howells 	int ret;
2238c3e34a4SDavid Howells 
224999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2258c3e34a4SDavid Howells 
226aa390bbeSDavid Howells 	call = rxrpc_alloc_client_call(rx, srx, gfp);
2278c3e34a4SDavid Howells 	if (IS_ERR(call)) {
2288c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2298c3e34a4SDavid Howells 		return call;
2308c3e34a4SDavid Howells 	}
2318c3e34a4SDavid Howells 
232999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2338c3e34a4SDavid Howells 	call->user_call_ID = user_call_ID;
2348c3e34a4SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
2358c3e34a4SDavid Howells 
2368c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2378c3e34a4SDavid Howells 
2388c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2398c3e34a4SDavid Howells 	parent = NULL;
2408c3e34a4SDavid Howells 	while (*pp) {
2418c3e34a4SDavid Howells 		parent = *pp;
2428c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2438c3e34a4SDavid Howells 
2448c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2458c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2468c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2478c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2488c3e34a4SDavid Howells 		else
2498c3e34a4SDavid Howells 			goto found_user_ID_now_present;
2508c3e34a4SDavid Howells 	}
2518c3e34a4SDavid Howells 
2528c3e34a4SDavid Howells 	rxrpc_get_call(call);
2538c3e34a4SDavid Howells 
2548c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2558c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
2568c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2578c3e34a4SDavid Howells 
2588c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
2598c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
2608c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
2618c3e34a4SDavid Howells 
262aa390bbeSDavid Howells 	ret = rxrpc_begin_client_call(call, cp, srx, gfp);
263999b69f8SDavid Howells 	if (ret < 0)
264999b69f8SDavid Howells 		goto error;
265999b69f8SDavid Howells 
2668c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2678c3e34a4SDavid Howells 
2688c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2698c3e34a4SDavid Howells 	return call;
2708c3e34a4SDavid Howells 
271999b69f8SDavid Howells error:
272999b69f8SDavid Howells 	write_lock(&rx->call_lock);
273999b69f8SDavid Howells 	rb_erase(&call->sock_node, &rx->calls);
274999b69f8SDavid Howells 	write_unlock(&rx->call_lock);
275999b69f8SDavid Howells 	rxrpc_put_call(call);
276999b69f8SDavid Howells 
277999b69f8SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
278d1e858c5SDavid Howells 	list_del_init(&call->link);
279999b69f8SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
280999b69f8SDavid Howells 
28117b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
282d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
283999b69f8SDavid Howells 	rxrpc_put_call(call);
284999b69f8SDavid Howells 	_leave(" = %d", ret);
285999b69f8SDavid Howells 	return ERR_PTR(ret);
286999b69f8SDavid Howells 
2878c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2888c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2898c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2908c3e34a4SDavid Howells 	 * same time in different threads.
2918c3e34a4SDavid Howells 	 */
2928c3e34a4SDavid Howells found_user_ID_now_present:
2938c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
29417b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
295d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
2968c3e34a4SDavid Howells 	rxrpc_put_call(call);
2978c3e34a4SDavid Howells 	_leave(" = -EEXIST [%p]", call);
2988c3e34a4SDavid Howells 	return ERR_PTR(-EEXIST);
2998c3e34a4SDavid Howells }
3008c3e34a4SDavid Howells 
3018c3e34a4SDavid Howells /*
3028c3e34a4SDavid Howells  * set up an incoming call
3038c3e34a4SDavid Howells  * - called in process context with IRQs enabled
3048c3e34a4SDavid Howells  */
3058c3e34a4SDavid Howells struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
3068c3e34a4SDavid Howells 				       struct rxrpc_connection *conn,
30742886ffeSDavid Howells 				       struct sk_buff *skb)
3088c3e34a4SDavid Howells {
30942886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3108c3e34a4SDavid Howells 	struct rxrpc_call *call, *candidate;
311a1399f8bSDavid Howells 	u32 call_id, chan;
3128c3e34a4SDavid Howells 
3138c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
3148c3e34a4SDavid Howells 
3158c3e34a4SDavid Howells 	ASSERT(rx != NULL);
3168c3e34a4SDavid Howells 
3178c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
3188c3e34a4SDavid Howells 	if (!candidate)
3198c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
3208c3e34a4SDavid Howells 
321a1399f8bSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
3228c3e34a4SDavid Howells 	candidate->socket	= rx;
3238c3e34a4SDavid Howells 	candidate->conn		= conn;
324df5d8bf7SDavid Howells 	candidate->peer		= conn->params.peer;
32542886ffeSDavid Howells 	candidate->cid		= sp->hdr.cid;
32642886ffeSDavid Howells 	candidate->call_id	= sp->hdr.callNumber;
3278c3e34a4SDavid Howells 	candidate->rx_data_post	= 0;
3288c3e34a4SDavid Howells 	candidate->state	= RXRPC_CALL_SERVER_ACCEPTING;
329dabe5a79SDavid Howells 	candidate->flags	|= (1 << RXRPC_CALL_IS_SERVICE);
3308c3e34a4SDavid Howells 	if (conn->security_ix > 0)
3318c3e34a4SDavid Howells 		candidate->state = RXRPC_CALL_SERVER_SECURING;
3328c3e34a4SDavid Howells 
333a1399f8bSDavid Howells 	spin_lock(&conn->channel_lock);
3348c3e34a4SDavid Howells 
3358c3e34a4SDavid Howells 	/* set the channel for this call */
336a1399f8bSDavid Howells 	call = rcu_dereference_protected(conn->channels[chan].call,
337a1399f8bSDavid Howells 					 lockdep_is_held(&conn->channel_lock));
338a1399f8bSDavid Howells 
33901a90a45SDavid Howells 	_debug("channel[%u] is %p", candidate->cid & RXRPC_CHANNELMASK, call);
34042886ffeSDavid Howells 	if (call && call->call_id == sp->hdr.callNumber) {
3418c3e34a4SDavid Howells 		/* already set; must've been a duplicate packet */
3428c3e34a4SDavid Howells 		_debug("extant call [%d]", call->state);
3438c3e34a4SDavid Howells 		ASSERTCMP(call->conn, ==, conn);
3448c3e34a4SDavid Howells 
3458c3e34a4SDavid Howells 		read_lock(&call->state_lock);
3468c3e34a4SDavid Howells 		switch (call->state) {
3478c3e34a4SDavid Howells 		case RXRPC_CALL_LOCALLY_ABORTED:
3488c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
3498c3e34a4SDavid Howells 				rxrpc_queue_call(call);
3508c3e34a4SDavid Howells 		case RXRPC_CALL_REMOTELY_ABORTED:
3518c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3528c3e34a4SDavid Howells 			goto aborted_call;
3538c3e34a4SDavid Howells 		default:
3548c3e34a4SDavid Howells 			rxrpc_get_call(call);
3558c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3568c3e34a4SDavid Howells 			goto extant_call;
3578c3e34a4SDavid Howells 		}
3588c3e34a4SDavid Howells 	}
3598c3e34a4SDavid Howells 
3608c3e34a4SDavid Howells 	if (call) {
3618c3e34a4SDavid Howells 		/* it seems the channel is still in use from the previous call
3628c3e34a4SDavid Howells 		 * - ditch the old binding if its call is now complete */
3638c3e34a4SDavid Howells 		_debug("CALL: %u { %s }",
3648c3e34a4SDavid Howells 		       call->debug_id, rxrpc_call_states[call->state]);
3658c3e34a4SDavid Howells 
366f5c17aaeSDavid Howells 		if (call->state == RXRPC_CALL_COMPLETE) {
36745025bceSDavid Howells 			__rxrpc_disconnect_call(conn, call);
3688c3e34a4SDavid Howells 		} else {
369a1399f8bSDavid Howells 			spin_unlock(&conn->channel_lock);
3708c3e34a4SDavid Howells 			kmem_cache_free(rxrpc_call_jar, candidate);
3718c3e34a4SDavid Howells 			_leave(" = -EBUSY");
3728c3e34a4SDavid Howells 			return ERR_PTR(-EBUSY);
3738c3e34a4SDavid Howells 		}
3748c3e34a4SDavid Howells 	}
3758c3e34a4SDavid Howells 
3768c3e34a4SDavid Howells 	/* check the call number isn't duplicate */
3778c3e34a4SDavid Howells 	_debug("check dup");
37842886ffeSDavid Howells 	call_id = sp->hdr.callNumber;
3798c3e34a4SDavid Howells 
380a1399f8bSDavid Howells 	/* We just ignore calls prior to the current call ID.  Terminated calls
381a1399f8bSDavid Howells 	 * are handled via the connection.
3828c3e34a4SDavid Howells 	 */
383a1399f8bSDavid Howells 	if (call_id <= conn->channels[chan].call_counter)
384a1399f8bSDavid Howells 		goto old_call; /* TODO: Just drop packet */
3858c3e34a4SDavid Howells 
3868c3e34a4SDavid Howells 	/* make the call available */
3878c3e34a4SDavid Howells 	_debug("new call");
3888c3e34a4SDavid Howells 	call = candidate;
3898c3e34a4SDavid Howells 	candidate = NULL;
390a1399f8bSDavid Howells 	conn->channels[chan].call_counter = call_id;
391a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3928c3e34a4SDavid Howells 	sock_hold(&rx->sk);
3935627cc8bSDavid Howells 	rxrpc_get_connection(conn);
394df5d8bf7SDavid Howells 	rxrpc_get_peer(call->peer);
395a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
3968c3e34a4SDavid Howells 
39785f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
39885f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
39985f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
4008c3e34a4SDavid Howells 
4018c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
4028c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
4038c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
4048c3e34a4SDavid Howells 
40519ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
4068c3e34a4SDavid Howells 
4078c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
4088c3e34a4SDavid Howells 
4098c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
4108c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
4118c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
4128c3e34a4SDavid Howells 	return call;
4138c3e34a4SDavid Howells 
4148c3e34a4SDavid Howells extant_call:
415a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4168c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4178c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
4188c3e34a4SDavid Howells 	return call;
4198c3e34a4SDavid Howells 
4208c3e34a4SDavid Howells aborted_call:
421a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4228c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4238c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
4248c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
4258c3e34a4SDavid Howells 
4268c3e34a4SDavid Howells old_call:
427a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4288c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4298c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
4308c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
4318c3e34a4SDavid Howells }
4328c3e34a4SDavid Howells 
4338c3e34a4SDavid Howells /*
4348c3e34a4SDavid Howells  * detach a call from a socket and set up for release
4358c3e34a4SDavid Howells  */
4368c3e34a4SDavid Howells void rxrpc_release_call(struct rxrpc_call *call)
4378c3e34a4SDavid Howells {
4388c3e34a4SDavid Howells 	struct rxrpc_connection *conn = call->conn;
4398c3e34a4SDavid Howells 	struct rxrpc_sock *rx = call->socket;
4408c3e34a4SDavid Howells 
4418c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
4428c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
4438c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
4448c3e34a4SDavid Howells 	       call->rx_first_oos);
4458c3e34a4SDavid Howells 
4468c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4478c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4488c3e34a4SDavid Howells 		BUG();
4498c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4508c3e34a4SDavid Howells 
4518c3e34a4SDavid Howells 	/* dissociate from the socket
4528c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
4538c3e34a4SDavid Howells 	 */
4548c3e34a4SDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4558c3e34a4SDavid Howells 
456e653cfe4SDavid Howells 	spin_lock(&conn->params.peer->lock);
457e653cfe4SDavid Howells 	hlist_del_init(&call->error_link);
458e653cfe4SDavid Howells 	spin_unlock(&conn->params.peer->lock);
459e653cfe4SDavid Howells 
4608c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
4618c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
4628c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4638c3e34a4SDavid Howells 		       call, call->events, call->flags);
4648c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
4658c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
4668c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
4678c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4688c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4698c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4708c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
4718c3e34a4SDavid Howells 	}
4728c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
4738c3e34a4SDavid Howells 
4748c3e34a4SDavid Howells 	/* free up the channel for reuse */
475a1399f8bSDavid Howells 	write_lock_bh(&call->state_lock);
4768c3e34a4SDavid Howells 
4778c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
4788c3e34a4SDavid Howells 	    call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
4798c3e34a4SDavid Howells 		_debug("+++ ABORTING STATE %d +++\n", call->state);
480f5c17aaeSDavid Howells 		__rxrpc_abort_call(call, RX_CALL_DEAD, ECONNRESET);
4818c3e34a4SDavid Howells 	}
482a1399f8bSDavid Howells 	write_unlock_bh(&call->state_lock);
4838c3e34a4SDavid Howells 
484e653cfe4SDavid Howells 	rxrpc_disconnect_call(call);
485e653cfe4SDavid Howells 
4868c3e34a4SDavid Howells 	/* clean up the Rx queue */
4878c3e34a4SDavid Howells 	if (!skb_queue_empty(&call->rx_queue) ||
4888c3e34a4SDavid Howells 	    !skb_queue_empty(&call->rx_oos_queue)) {
4898c3e34a4SDavid Howells 		struct rxrpc_skb_priv *sp;
4908c3e34a4SDavid Howells 		struct sk_buff *skb;
4918c3e34a4SDavid Howells 
4928c3e34a4SDavid Howells 		_debug("purge Rx queues");
4938c3e34a4SDavid Howells 
4948c3e34a4SDavid Howells 		spin_lock_bh(&call->lock);
4958c3e34a4SDavid Howells 		while ((skb = skb_dequeue(&call->rx_queue)) ||
4968c3e34a4SDavid Howells 		       (skb = skb_dequeue(&call->rx_oos_queue))) {
4978c3e34a4SDavid Howells 			spin_unlock_bh(&call->lock);
4988c3e34a4SDavid Howells 
49955cae7a4SArnd Bergmann 			sp = rxrpc_skb(skb);
5008c3e34a4SDavid Howells 			_debug("- zap %s %%%u #%u",
5018c3e34a4SDavid Howells 			       rxrpc_pkts[sp->hdr.type],
5028c3e34a4SDavid Howells 			       sp->hdr.serial, sp->hdr.seq);
5038c3e34a4SDavid Howells 			rxrpc_free_skb(skb);
5048c3e34a4SDavid Howells 			spin_lock_bh(&call->lock);
5058c3e34a4SDavid Howells 		}
5068c3e34a4SDavid Howells 		spin_unlock_bh(&call->lock);
5078c3e34a4SDavid Howells 
5088c3e34a4SDavid Howells 		ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
5098c3e34a4SDavid Howells 	}
5108c3e34a4SDavid Howells 
5118c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
5128c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
5138c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
5148c3e34a4SDavid Howells 	call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
5158c3e34a4SDavid Howells 	add_timer(&call->deadspan);
5168c3e34a4SDavid Howells 
5178c3e34a4SDavid Howells 	_leave("");
5188c3e34a4SDavid Howells }
5198c3e34a4SDavid Howells 
5208c3e34a4SDavid Howells /*
5218c3e34a4SDavid Howells  * handle a dead call being ready for reaping
5228c3e34a4SDavid Howells  */
5238c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call)
5248c3e34a4SDavid Howells {
5258c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
5268c3e34a4SDavid Howells 
5278c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
5288c3e34a4SDavid Howells 
5298c3e34a4SDavid Howells 	write_lock_bh(&call->state_lock);
5308c3e34a4SDavid Howells 	call->state = RXRPC_CALL_DEAD;
5318c3e34a4SDavid Howells 	write_unlock_bh(&call->state_lock);
5328c3e34a4SDavid Howells 	rxrpc_put_call(call);
5338c3e34a4SDavid Howells }
5348c3e34a4SDavid Howells 
5358c3e34a4SDavid Howells /*
5368c3e34a4SDavid Howells  * mark a call as to be released, aborting it if it's still in progress
5378c3e34a4SDavid Howells  * - called with softirqs disabled
5388c3e34a4SDavid Howells  */
5398c3e34a4SDavid Howells static void rxrpc_mark_call_released(struct rxrpc_call *call)
5408c3e34a4SDavid Howells {
5418c3e34a4SDavid Howells 	bool sched;
5428c3e34a4SDavid Howells 
5438c3e34a4SDavid Howells 	write_lock(&call->state_lock);
5448c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_DEAD) {
545f5c17aaeSDavid Howells 		sched = __rxrpc_abort_call(call, RX_CALL_DEAD, ECONNRESET);
5468c3e34a4SDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
5478c3e34a4SDavid Howells 			sched = true;
5488c3e34a4SDavid Howells 	}
5498c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
550f5c17aaeSDavid Howells 	if (sched)
551f5c17aaeSDavid Howells 		rxrpc_queue_call(call);
5528c3e34a4SDavid Howells }
5538c3e34a4SDavid Howells 
5548c3e34a4SDavid Howells /*
5558c3e34a4SDavid Howells  * release all the calls associated with a socket
5568c3e34a4SDavid Howells  */
5578c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
5588c3e34a4SDavid Howells {
5598c3e34a4SDavid Howells 	struct rxrpc_call *call;
5608c3e34a4SDavid Howells 	struct rb_node *p;
5618c3e34a4SDavid Howells 
5628c3e34a4SDavid Howells 	_enter("%p", rx);
5638c3e34a4SDavid Howells 
5648c3e34a4SDavid Howells 	read_lock_bh(&rx->call_lock);
5658c3e34a4SDavid Howells 
5668c3e34a4SDavid Howells 	/* kill the not-yet-accepted incoming calls */
5678c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->secureq, accept_link) {
5688c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
5698c3e34a4SDavid Howells 	}
5708c3e34a4SDavid Howells 
5718c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->acceptq, accept_link) {
5728c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
5738c3e34a4SDavid Howells 	}
5748c3e34a4SDavid Howells 
575f36b5e44SDavid Howells 	/* mark all the calls as no longer wanting incoming packets */
576f36b5e44SDavid Howells 	for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
577f36b5e44SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
578f36b5e44SDavid Howells 		rxrpc_mark_call_released(call);
579f36b5e44SDavid Howells 	}
580f36b5e44SDavid Howells 
5818c3e34a4SDavid Howells 	read_unlock_bh(&rx->call_lock);
5828c3e34a4SDavid Howells 	_leave("");
5838c3e34a4SDavid Howells }
5848c3e34a4SDavid Howells 
5858c3e34a4SDavid Howells /*
5868c3e34a4SDavid Howells  * release a call
5878c3e34a4SDavid Howells  */
5888c3e34a4SDavid Howells void __rxrpc_put_call(struct rxrpc_call *call)
5898c3e34a4SDavid Howells {
5908c3e34a4SDavid Howells 	ASSERT(call != NULL);
5918c3e34a4SDavid Howells 
5928c3e34a4SDavid Howells 	_enter("%p{u=%d}", call, atomic_read(&call->usage));
5938c3e34a4SDavid Howells 
5948c3e34a4SDavid Howells 	ASSERTCMP(atomic_read(&call->usage), >, 0);
5958c3e34a4SDavid Howells 
5968c3e34a4SDavid Howells 	if (atomic_dec_and_test(&call->usage)) {
5978c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
598372ee163SDavid Howells 		WARN_ON(atomic_read(&call->skb_count) != 0);
5998c3e34a4SDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
6008c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
6018c3e34a4SDavid Howells 	}
6028c3e34a4SDavid Howells 	_leave("");
6038c3e34a4SDavid Howells }
6048c3e34a4SDavid Howells 
6058c3e34a4SDavid Howells /*
606dee46364SDavid Howells  * Final call destruction under RCU.
607dee46364SDavid Howells  */
608dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
609dee46364SDavid Howells {
610dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
611dee46364SDavid Howells 
612dee46364SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
613df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
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 
68001a90a45SDavid Howells 	_enter("%p{%d,%x,%p}",
68101a90a45SDavid Howells 	       call, atomic_read(&call->usage), call->cid, 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 
749f5c17aaeSDavid Howells 	_enter("{%d}", call->debug_id);
750f5c17aaeSDavid Howells 
7518c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7528c3e34a4SDavid Howells 		return;
7538c3e34a4SDavid Howells 
7548c3e34a4SDavid Howells 	set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
7558c3e34a4SDavid Howells 	rxrpc_queue_call(call);
7568c3e34a4SDavid Howells }
7578c3e34a4SDavid Howells 
7588c3e34a4SDavid Howells /*
7598c3e34a4SDavid Howells  * handle resend timer expiry
7608c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
7618c3e34a4SDavid Howells  */
7628c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
7638c3e34a4SDavid Howells {
7648c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7658c3e34a4SDavid Howells 
7668c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7678c3e34a4SDavid Howells 
7688c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7698c3e34a4SDavid Howells 		return;
7708c3e34a4SDavid Howells 
7718c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
7728c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
7738c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7748c3e34a4SDavid Howells }
7758c3e34a4SDavid Howells 
7768c3e34a4SDavid Howells /*
7778c3e34a4SDavid Howells  * handle ACK timer expiry
7788c3e34a4SDavid Howells  */
7798c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
7808c3e34a4SDavid Howells {
7818c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7828c3e34a4SDavid Howells 
7838c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7848c3e34a4SDavid Howells 
7858c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7868c3e34a4SDavid Howells 		return;
7878c3e34a4SDavid Howells 
788f5c17aaeSDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
7898c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7908c3e34a4SDavid Howells }
791