xref: /openbmc/linux/net/rxrpc/call_object.c (revision 45025bce)
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);
13045025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1318c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
1328c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_queue);
1338c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_oos_queue);
13445025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1358c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
1368c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1378c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1388c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
1398c3e34a4SDavid Howells 
1408c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1418c3e34a4SDavid Howells 
1428c3e34a4SDavid Howells 	call->rx_data_expect = 1;
1438c3e34a4SDavid Howells 	call->rx_data_eaten = 0;
1448c3e34a4SDavid Howells 	call->rx_first_oos = 0;
1458c3e34a4SDavid Howells 	call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
1468c3e34a4SDavid Howells 	call->creation_jif = jiffies;
1478c3e34a4SDavid Howells 	return call;
1488c3e34a4SDavid Howells }
1498c3e34a4SDavid Howells 
1508c3e34a4SDavid Howells /*
151999b69f8SDavid Howells  * Allocate a new client call.
1528c3e34a4SDavid Howells  */
153aa390bbeSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
154999b69f8SDavid Howells 						  struct sockaddr_rxrpc *srx,
1558c3e34a4SDavid Howells 						  gfp_t gfp)
1568c3e34a4SDavid Howells {
1578c3e34a4SDavid Howells 	struct rxrpc_call *call;
1588c3e34a4SDavid Howells 
1598c3e34a4SDavid Howells 	_enter("");
1608c3e34a4SDavid Howells 
161999b69f8SDavid Howells 	ASSERT(rx->local != NULL);
1628c3e34a4SDavid Howells 
1638c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1648c3e34a4SDavid Howells 	if (!call)
1658c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
166999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
1678c3e34a4SDavid Howells 
1688c3e34a4SDavid Howells 	sock_hold(&rx->sk);
1698c3e34a4SDavid Howells 	call->socket = rx;
1708c3e34a4SDavid Howells 	call->rx_data_post = 1;
171999b69f8SDavid Howells 	call->service_id = srx->srx_service;
172999b69f8SDavid Howells 
173999b69f8SDavid Howells 	_leave(" = %p", call);
174999b69f8SDavid Howells 	return call;
175999b69f8SDavid Howells }
176999b69f8SDavid Howells 
177999b69f8SDavid Howells /*
178999b69f8SDavid Howells  * Begin client call.
179999b69f8SDavid Howells  */
180999b69f8SDavid Howells static int rxrpc_begin_client_call(struct rxrpc_call *call,
181999b69f8SDavid Howells 				   struct rxrpc_conn_parameters *cp,
182999b69f8SDavid Howells 				   struct sockaddr_rxrpc *srx,
183999b69f8SDavid Howells 				   gfp_t gfp)
184999b69f8SDavid Howells {
185999b69f8SDavid Howells 	int ret;
186999b69f8SDavid Howells 
187999b69f8SDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
188999b69f8SDavid Howells 	 * including channel number and call ID.
189999b69f8SDavid Howells 	 */
190aa390bbeSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
191999b69f8SDavid Howells 	if (ret < 0)
192999b69f8SDavid Howells 		return ret;
193999b69f8SDavid Howells 
194999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
195999b69f8SDavid Howells 
19685f32278SDavid Howells 	spin_lock(&call->conn->params.peer->lock);
19785f32278SDavid Howells 	hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
19885f32278SDavid Howells 	spin_unlock(&call->conn->params.peer->lock);
1998c3e34a4SDavid Howells 
2008c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
2018c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
202999b69f8SDavid Howells 	return 0;
2038c3e34a4SDavid Howells }
2048c3e34a4SDavid Howells 
2058c3e34a4SDavid Howells /*
2068c3e34a4SDavid Howells  * set up a call for the given data
2078c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2088c3e34a4SDavid Howells  */
2098c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
21019ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
211999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2128c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2138c3e34a4SDavid Howells 					 gfp_t gfp)
2148c3e34a4SDavid Howells {
2158c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2168c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
217999b69f8SDavid Howells 	int ret;
2188c3e34a4SDavid Howells 
219999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2208c3e34a4SDavid Howells 
221aa390bbeSDavid Howells 	call = rxrpc_alloc_client_call(rx, srx, gfp);
2228c3e34a4SDavid Howells 	if (IS_ERR(call)) {
2238c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2248c3e34a4SDavid Howells 		return call;
2258c3e34a4SDavid Howells 	}
2268c3e34a4SDavid Howells 
227999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2288c3e34a4SDavid Howells 	call->user_call_ID = user_call_ID;
2298c3e34a4SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
2308c3e34a4SDavid Howells 
2318c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2328c3e34a4SDavid Howells 
2338c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2348c3e34a4SDavid Howells 	parent = NULL;
2358c3e34a4SDavid Howells 	while (*pp) {
2368c3e34a4SDavid Howells 		parent = *pp;
2378c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2388c3e34a4SDavid Howells 
2398c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2408c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2418c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2428c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2438c3e34a4SDavid Howells 		else
2448c3e34a4SDavid Howells 			goto found_user_ID_now_present;
2458c3e34a4SDavid Howells 	}
2468c3e34a4SDavid Howells 
2478c3e34a4SDavid Howells 	rxrpc_get_call(call);
2488c3e34a4SDavid Howells 
2498c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2508c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
2518c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2528c3e34a4SDavid Howells 
2538c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
2548c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
2558c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
2568c3e34a4SDavid Howells 
257aa390bbeSDavid Howells 	ret = rxrpc_begin_client_call(call, cp, srx, gfp);
258999b69f8SDavid Howells 	if (ret < 0)
259999b69f8SDavid Howells 		goto error;
260999b69f8SDavid Howells 
2618c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2628c3e34a4SDavid Howells 
2638c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2648c3e34a4SDavid Howells 	return call;
2658c3e34a4SDavid Howells 
266999b69f8SDavid Howells error:
267999b69f8SDavid Howells 	write_lock(&rx->call_lock);
268999b69f8SDavid Howells 	rb_erase(&call->sock_node, &rx->calls);
269999b69f8SDavid Howells 	write_unlock(&rx->call_lock);
270999b69f8SDavid Howells 	rxrpc_put_call(call);
271999b69f8SDavid Howells 
272999b69f8SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
273d1e858c5SDavid Howells 	list_del_init(&call->link);
274999b69f8SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
275999b69f8SDavid Howells 
27617b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
277d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
278999b69f8SDavid Howells 	rxrpc_put_call(call);
279999b69f8SDavid Howells 	_leave(" = %d", ret);
280999b69f8SDavid Howells 	return ERR_PTR(ret);
281999b69f8SDavid Howells 
2828c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2838c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2848c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2858c3e34a4SDavid Howells 	 * same time in different threads.
2868c3e34a4SDavid Howells 	 */
2878c3e34a4SDavid Howells found_user_ID_now_present:
2888c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
28917b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
290d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
2918c3e34a4SDavid Howells 	rxrpc_put_call(call);
2928c3e34a4SDavid Howells 	_leave(" = -EEXIST [%p]", call);
2938c3e34a4SDavid Howells 	return ERR_PTR(-EEXIST);
2948c3e34a4SDavid Howells }
2958c3e34a4SDavid Howells 
2968c3e34a4SDavid Howells /*
2978c3e34a4SDavid Howells  * set up an incoming call
2988c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2998c3e34a4SDavid Howells  */
3008c3e34a4SDavid Howells struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
3018c3e34a4SDavid Howells 				       struct rxrpc_connection *conn,
30242886ffeSDavid Howells 				       struct sk_buff *skb)
3038c3e34a4SDavid Howells {
30442886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3058c3e34a4SDavid Howells 	struct rxrpc_call *call, *candidate;
306a1399f8bSDavid Howells 	u32 call_id, chan;
3078c3e34a4SDavid Howells 
3088c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
3098c3e34a4SDavid Howells 
3108c3e34a4SDavid Howells 	ASSERT(rx != NULL);
3118c3e34a4SDavid Howells 
3128c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
3138c3e34a4SDavid Howells 	if (!candidate)
3148c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
3158c3e34a4SDavid Howells 
316a1399f8bSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
3178c3e34a4SDavid Howells 	candidate->socket	= rx;
3188c3e34a4SDavid Howells 	candidate->conn		= conn;
319df5d8bf7SDavid Howells 	candidate->peer		= conn->params.peer;
32042886ffeSDavid Howells 	candidate->cid		= sp->hdr.cid;
32142886ffeSDavid Howells 	candidate->call_id	= sp->hdr.callNumber;
3228c3e34a4SDavid Howells 	candidate->rx_data_post	= 0;
3238c3e34a4SDavid Howells 	candidate->state	= RXRPC_CALL_SERVER_ACCEPTING;
324dabe5a79SDavid Howells 	candidate->flags	|= (1 << RXRPC_CALL_IS_SERVICE);
3258c3e34a4SDavid Howells 	if (conn->security_ix > 0)
3268c3e34a4SDavid Howells 		candidate->state = RXRPC_CALL_SERVER_SECURING;
3278c3e34a4SDavid Howells 
328a1399f8bSDavid Howells 	spin_lock(&conn->channel_lock);
3298c3e34a4SDavid Howells 
3308c3e34a4SDavid Howells 	/* set the channel for this call */
331a1399f8bSDavid Howells 	call = rcu_dereference_protected(conn->channels[chan].call,
332a1399f8bSDavid Howells 					 lockdep_is_held(&conn->channel_lock));
333a1399f8bSDavid Howells 
33401a90a45SDavid Howells 	_debug("channel[%u] is %p", candidate->cid & RXRPC_CHANNELMASK, call);
33542886ffeSDavid Howells 	if (call && call->call_id == sp->hdr.callNumber) {
3368c3e34a4SDavid Howells 		/* already set; must've been a duplicate packet */
3378c3e34a4SDavid Howells 		_debug("extant call [%d]", call->state);
3388c3e34a4SDavid Howells 		ASSERTCMP(call->conn, ==, conn);
3398c3e34a4SDavid Howells 
3408c3e34a4SDavid Howells 		read_lock(&call->state_lock);
3418c3e34a4SDavid Howells 		switch (call->state) {
3428c3e34a4SDavid Howells 		case RXRPC_CALL_LOCALLY_ABORTED:
3438c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
3448c3e34a4SDavid Howells 				rxrpc_queue_call(call);
3458c3e34a4SDavid Howells 		case RXRPC_CALL_REMOTELY_ABORTED:
3468c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3478c3e34a4SDavid Howells 			goto aborted_call;
3488c3e34a4SDavid Howells 		default:
3498c3e34a4SDavid Howells 			rxrpc_get_call(call);
3508c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3518c3e34a4SDavid Howells 			goto extant_call;
3528c3e34a4SDavid Howells 		}
3538c3e34a4SDavid Howells 	}
3548c3e34a4SDavid Howells 
3558c3e34a4SDavid Howells 	if (call) {
3568c3e34a4SDavid Howells 		/* it seems the channel is still in use from the previous call
3578c3e34a4SDavid Howells 		 * - ditch the old binding if its call is now complete */
3588c3e34a4SDavid Howells 		_debug("CALL: %u { %s }",
3598c3e34a4SDavid Howells 		       call->debug_id, rxrpc_call_states[call->state]);
3608c3e34a4SDavid Howells 
3618c3e34a4SDavid Howells 		if (call->state >= RXRPC_CALL_COMPLETE) {
36245025bceSDavid Howells 			__rxrpc_disconnect_call(conn, call);
3638c3e34a4SDavid Howells 		} else {
364a1399f8bSDavid Howells 			spin_unlock(&conn->channel_lock);
3658c3e34a4SDavid Howells 			kmem_cache_free(rxrpc_call_jar, candidate);
3668c3e34a4SDavid Howells 			_leave(" = -EBUSY");
3678c3e34a4SDavid Howells 			return ERR_PTR(-EBUSY);
3688c3e34a4SDavid Howells 		}
3698c3e34a4SDavid Howells 	}
3708c3e34a4SDavid Howells 
3718c3e34a4SDavid Howells 	/* check the call number isn't duplicate */
3728c3e34a4SDavid Howells 	_debug("check dup");
37342886ffeSDavid Howells 	call_id = sp->hdr.callNumber;
3748c3e34a4SDavid Howells 
375a1399f8bSDavid Howells 	/* We just ignore calls prior to the current call ID.  Terminated calls
376a1399f8bSDavid Howells 	 * are handled via the connection.
3778c3e34a4SDavid Howells 	 */
378a1399f8bSDavid Howells 	if (call_id <= conn->channels[chan].call_counter)
379a1399f8bSDavid Howells 		goto old_call; /* TODO: Just drop packet */
3808c3e34a4SDavid Howells 
3818c3e34a4SDavid Howells 	/* make the call available */
3828c3e34a4SDavid Howells 	_debug("new call");
3838c3e34a4SDavid Howells 	call = candidate;
3848c3e34a4SDavid Howells 	candidate = NULL;
385a1399f8bSDavid Howells 	conn->channels[chan].call_counter = call_id;
386a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3878c3e34a4SDavid Howells 	sock_hold(&rx->sk);
3885627cc8bSDavid Howells 	rxrpc_get_connection(conn);
389df5d8bf7SDavid Howells 	rxrpc_get_peer(call->peer);
390a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
3918c3e34a4SDavid Howells 
39285f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
39385f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
39485f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3958c3e34a4SDavid Howells 
3968c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
3978c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
3988c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
3998c3e34a4SDavid Howells 
40019ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
4018c3e34a4SDavid Howells 
4028c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
4038c3e34a4SDavid Howells 
4048c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
4058c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
4068c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
4078c3e34a4SDavid Howells 	return call;
4088c3e34a4SDavid Howells 
4098c3e34a4SDavid Howells extant_call:
410a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4118c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4128c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
4138c3e34a4SDavid Howells 	return call;
4148c3e34a4SDavid Howells 
4158c3e34a4SDavid Howells aborted_call:
416a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4178c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4188c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
4198c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
4208c3e34a4SDavid Howells 
4218c3e34a4SDavid Howells old_call:
422a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4238c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4248c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
4258c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
4268c3e34a4SDavid Howells }
4278c3e34a4SDavid Howells 
4288c3e34a4SDavid Howells /*
4298c3e34a4SDavid Howells  * detach a call from a socket and set up for release
4308c3e34a4SDavid Howells  */
4318c3e34a4SDavid Howells void rxrpc_release_call(struct rxrpc_call *call)
4328c3e34a4SDavid Howells {
4338c3e34a4SDavid Howells 	struct rxrpc_connection *conn = call->conn;
4348c3e34a4SDavid Howells 	struct rxrpc_sock *rx = call->socket;
4358c3e34a4SDavid Howells 
4368c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
4378c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
4388c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
4398c3e34a4SDavid Howells 	       call->rx_first_oos);
4408c3e34a4SDavid Howells 
4418c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4428c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4438c3e34a4SDavid Howells 		BUG();
4448c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4458c3e34a4SDavid Howells 
4468c3e34a4SDavid Howells 	/* dissociate from the socket
4478c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
4488c3e34a4SDavid Howells 	 */
4498c3e34a4SDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4508c3e34a4SDavid Howells 
451e653cfe4SDavid Howells 	spin_lock(&conn->params.peer->lock);
452e653cfe4SDavid Howells 	hlist_del_init(&call->error_link);
453e653cfe4SDavid Howells 	spin_unlock(&conn->params.peer->lock);
454e653cfe4SDavid Howells 
4558c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
4568c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
4578c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4588c3e34a4SDavid Howells 		       call, call->events, call->flags);
4598c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
4608c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
4618c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
4628c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4638c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4648c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4658c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
4668c3e34a4SDavid Howells 	}
4678c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
4688c3e34a4SDavid Howells 
4698c3e34a4SDavid Howells 	/* free up the channel for reuse */
470a1399f8bSDavid Howells 	write_lock_bh(&call->state_lock);
4718c3e34a4SDavid Howells 
4728c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
4738c3e34a4SDavid Howells 	    call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
4748c3e34a4SDavid Howells 		_debug("+++ ABORTING STATE %d +++\n", call->state);
4758c3e34a4SDavid Howells 		call->state = RXRPC_CALL_LOCALLY_ABORTED;
4768c3e34a4SDavid Howells 		call->local_abort = RX_CALL_DEAD;
4778c3e34a4SDavid Howells 	}
478a1399f8bSDavid Howells 	write_unlock_bh(&call->state_lock);
4798c3e34a4SDavid Howells 
480e653cfe4SDavid Howells 	rxrpc_disconnect_call(call);
481e653cfe4SDavid Howells 
4828c3e34a4SDavid Howells 	/* clean up the Rx queue */
4838c3e34a4SDavid Howells 	if (!skb_queue_empty(&call->rx_queue) ||
4848c3e34a4SDavid Howells 	    !skb_queue_empty(&call->rx_oos_queue)) {
4858c3e34a4SDavid Howells 		struct rxrpc_skb_priv *sp;
4868c3e34a4SDavid Howells 		struct sk_buff *skb;
4878c3e34a4SDavid Howells 
4888c3e34a4SDavid Howells 		_debug("purge Rx queues");
4898c3e34a4SDavid Howells 
4908c3e34a4SDavid Howells 		spin_lock_bh(&call->lock);
4918c3e34a4SDavid Howells 		while ((skb = skb_dequeue(&call->rx_queue)) ||
4928c3e34a4SDavid Howells 		       (skb = skb_dequeue(&call->rx_oos_queue))) {
4938c3e34a4SDavid Howells 			spin_unlock_bh(&call->lock);
4948c3e34a4SDavid Howells 
49555cae7a4SArnd Bergmann 			sp = rxrpc_skb(skb);
4968c3e34a4SDavid Howells 			_debug("- zap %s %%%u #%u",
4978c3e34a4SDavid Howells 			       rxrpc_pkts[sp->hdr.type],
4988c3e34a4SDavid Howells 			       sp->hdr.serial, sp->hdr.seq);
4998c3e34a4SDavid Howells 			rxrpc_free_skb(skb);
5008c3e34a4SDavid Howells 			spin_lock_bh(&call->lock);
5018c3e34a4SDavid Howells 		}
5028c3e34a4SDavid Howells 		spin_unlock_bh(&call->lock);
5038c3e34a4SDavid Howells 
5048c3e34a4SDavid Howells 		ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
5058c3e34a4SDavid Howells 	}
5068c3e34a4SDavid Howells 
5078c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
5088c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
5098c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
5108c3e34a4SDavid Howells 	call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
5118c3e34a4SDavid Howells 	add_timer(&call->deadspan);
5128c3e34a4SDavid Howells 
5138c3e34a4SDavid Howells 	_leave("");
5148c3e34a4SDavid Howells }
5158c3e34a4SDavid Howells 
5168c3e34a4SDavid Howells /*
5178c3e34a4SDavid Howells  * handle a dead call being ready for reaping
5188c3e34a4SDavid Howells  */
5198c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call)
5208c3e34a4SDavid Howells {
5218c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
5228c3e34a4SDavid Howells 
5238c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
5248c3e34a4SDavid Howells 
5258c3e34a4SDavid Howells 	write_lock_bh(&call->state_lock);
5268c3e34a4SDavid Howells 	call->state = RXRPC_CALL_DEAD;
5278c3e34a4SDavid Howells 	write_unlock_bh(&call->state_lock);
5288c3e34a4SDavid Howells 	rxrpc_put_call(call);
5298c3e34a4SDavid Howells }
5308c3e34a4SDavid Howells 
5318c3e34a4SDavid Howells /*
5328c3e34a4SDavid Howells  * mark a call as to be released, aborting it if it's still in progress
5338c3e34a4SDavid Howells  * - called with softirqs disabled
5348c3e34a4SDavid Howells  */
5358c3e34a4SDavid Howells static void rxrpc_mark_call_released(struct rxrpc_call *call)
5368c3e34a4SDavid Howells {
5378c3e34a4SDavid Howells 	bool sched;
5388c3e34a4SDavid Howells 
5398c3e34a4SDavid Howells 	write_lock(&call->state_lock);
5408c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_DEAD) {
5418c3e34a4SDavid Howells 		sched = false;
5428c3e34a4SDavid Howells 		if (call->state < RXRPC_CALL_COMPLETE) {
5438c3e34a4SDavid Howells 			_debug("abort call %p", call);
5448c3e34a4SDavid Howells 			call->state = RXRPC_CALL_LOCALLY_ABORTED;
5458c3e34a4SDavid Howells 			call->local_abort = RX_CALL_DEAD;
5468c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
5478c3e34a4SDavid Howells 				sched = true;
5488c3e34a4SDavid Howells 		}
5498c3e34a4SDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
5508c3e34a4SDavid Howells 			sched = true;
5518c3e34a4SDavid Howells 		if (sched)
5528c3e34a4SDavid Howells 			rxrpc_queue_call(call);
5538c3e34a4SDavid Howells 	}
5548c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
5558c3e34a4SDavid Howells }
5568c3e34a4SDavid Howells 
5578c3e34a4SDavid Howells /*
5588c3e34a4SDavid Howells  * release all the calls associated with a socket
5598c3e34a4SDavid Howells  */
5608c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
5618c3e34a4SDavid Howells {
5628c3e34a4SDavid Howells 	struct rxrpc_call *call;
5638c3e34a4SDavid Howells 	struct rb_node *p;
5648c3e34a4SDavid Howells 
5658c3e34a4SDavid Howells 	_enter("%p", rx);
5668c3e34a4SDavid Howells 
5678c3e34a4SDavid Howells 	read_lock_bh(&rx->call_lock);
5688c3e34a4SDavid Howells 
5698c3e34a4SDavid Howells 	/* kill the not-yet-accepted incoming calls */
5708c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->secureq, accept_link) {
5718c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
5728c3e34a4SDavid Howells 	}
5738c3e34a4SDavid Howells 
5748c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->acceptq, accept_link) {
5758c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
5768c3e34a4SDavid Howells 	}
5778c3e34a4SDavid Howells 
578f36b5e44SDavid Howells 	/* mark all the calls as no longer wanting incoming packets */
579f36b5e44SDavid Howells 	for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
580f36b5e44SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
581f36b5e44SDavid Howells 		rxrpc_mark_call_released(call);
582f36b5e44SDavid Howells 	}
583f36b5e44SDavid Howells 
5848c3e34a4SDavid Howells 	read_unlock_bh(&rx->call_lock);
5858c3e34a4SDavid Howells 	_leave("");
5868c3e34a4SDavid Howells }
5878c3e34a4SDavid Howells 
5888c3e34a4SDavid Howells /*
5898c3e34a4SDavid Howells  * release a call
5908c3e34a4SDavid Howells  */
5918c3e34a4SDavid Howells void __rxrpc_put_call(struct rxrpc_call *call)
5928c3e34a4SDavid Howells {
5938c3e34a4SDavid Howells 	ASSERT(call != NULL);
5948c3e34a4SDavid Howells 
5958c3e34a4SDavid Howells 	_enter("%p{u=%d}", call, atomic_read(&call->usage));
5968c3e34a4SDavid Howells 
5978c3e34a4SDavid Howells 	ASSERTCMP(atomic_read(&call->usage), >, 0);
5988c3e34a4SDavid Howells 
5998c3e34a4SDavid Howells 	if (atomic_dec_and_test(&call->usage)) {
6008c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
601372ee163SDavid Howells 		WARN_ON(atomic_read(&call->skb_count) != 0);
6028c3e34a4SDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
6038c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
6048c3e34a4SDavid Howells 	}
6058c3e34a4SDavid Howells 	_leave("");
6068c3e34a4SDavid Howells }
6078c3e34a4SDavid Howells 
6088c3e34a4SDavid Howells /*
609dee46364SDavid Howells  * Final call destruction under RCU.
610dee46364SDavid Howells  */
611dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
612dee46364SDavid Howells {
613dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
614dee46364SDavid Howells 
615dee46364SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
616df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
617dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
618dee46364SDavid Howells }
619dee46364SDavid Howells 
620dee46364SDavid Howells /*
6218c3e34a4SDavid Howells  * clean up a call
6228c3e34a4SDavid Howells  */
6238c3e34a4SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call)
6248c3e34a4SDavid Howells {
6258c3e34a4SDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
6268c3e34a4SDavid Howells 
6278c3e34a4SDavid Howells 	ASSERT(call->socket);
6288c3e34a4SDavid Howells 
6298c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
6308c3e34a4SDavid Howells 
6318c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
6328c3e34a4SDavid Howells 	del_timer_sync(&call->deadspan);
6338c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
6348c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
6358c3e34a4SDavid Howells 
6368c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
6378c3e34a4SDavid Howells 	ASSERTCMP(call->events, ==, 0);
6388c3e34a4SDavid Howells 	if (work_pending(&call->processor)) {
6398c3e34a4SDavid Howells 		_debug("defer destroy");
6408c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
6418c3e34a4SDavid Howells 		return;
6428c3e34a4SDavid Howells 	}
6438c3e34a4SDavid Howells 
644e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
6458c3e34a4SDavid Howells 
6468c3e34a4SDavid Howells 	if (call->acks_window) {
6478c3e34a4SDavid Howells 		_debug("kill Tx window %d",
6488c3e34a4SDavid Howells 		       CIRC_CNT(call->acks_head, call->acks_tail,
6498c3e34a4SDavid Howells 				call->acks_winsz));
6508c3e34a4SDavid Howells 		smp_mb();
6518c3e34a4SDavid Howells 		while (CIRC_CNT(call->acks_head, call->acks_tail,
6528c3e34a4SDavid Howells 				call->acks_winsz) > 0) {
6538c3e34a4SDavid Howells 			struct rxrpc_skb_priv *sp;
6548c3e34a4SDavid Howells 			unsigned long _skb;
6558c3e34a4SDavid Howells 
6568c3e34a4SDavid Howells 			_skb = call->acks_window[call->acks_tail] & ~1;
6578c3e34a4SDavid Howells 			sp = rxrpc_skb((struct sk_buff *)_skb);
6588c3e34a4SDavid Howells 			_debug("+++ clear Tx %u", sp->hdr.seq);
6598c3e34a4SDavid Howells 			rxrpc_free_skb((struct sk_buff *)_skb);
6608c3e34a4SDavid Howells 			call->acks_tail =
6618c3e34a4SDavid Howells 				(call->acks_tail + 1) & (call->acks_winsz - 1);
6628c3e34a4SDavid Howells 		}
6638c3e34a4SDavid Howells 
6648c3e34a4SDavid Howells 		kfree(call->acks_window);
6658c3e34a4SDavid Howells 	}
6668c3e34a4SDavid Howells 
6678c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
6688c3e34a4SDavid Howells 
6698c3e34a4SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
6708c3e34a4SDavid Howells 	ASSERT(skb_queue_empty(&call->rx_oos_queue));
6718c3e34a4SDavid Howells 	sock_put(&call->socket->sk);
672dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
6738c3e34a4SDavid Howells }
6748c3e34a4SDavid Howells 
6758c3e34a4SDavid Howells /*
6768c3e34a4SDavid Howells  * destroy a call
6778c3e34a4SDavid Howells  */
6788c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work)
6798c3e34a4SDavid Howells {
6808c3e34a4SDavid Howells 	struct rxrpc_call *call =
6818c3e34a4SDavid Howells 		container_of(work, struct rxrpc_call, destroyer);
6828c3e34a4SDavid Howells 
68301a90a45SDavid Howells 	_enter("%p{%d,%x,%p}",
68401a90a45SDavid Howells 	       call, atomic_read(&call->usage), call->cid, call->conn);
6858c3e34a4SDavid Howells 
6868c3e34a4SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
6878c3e34a4SDavid Howells 
6888c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
6898c3e34a4SDavid Howells 	list_del_init(&call->link);
6908c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
6918c3e34a4SDavid Howells 
6928c3e34a4SDavid Howells 	rxrpc_cleanup_call(call);
6938c3e34a4SDavid Howells 	_leave("");
6948c3e34a4SDavid Howells }
6958c3e34a4SDavid Howells 
6968c3e34a4SDavid Howells /*
6978c3e34a4SDavid Howells  * preemptively destroy all the call records from a transport endpoint rather
6988c3e34a4SDavid Howells  * than waiting for them to time out
6998c3e34a4SDavid Howells  */
7008c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
7018c3e34a4SDavid Howells {
7028c3e34a4SDavid Howells 	struct rxrpc_call *call;
7038c3e34a4SDavid Howells 
7048c3e34a4SDavid Howells 	_enter("");
7058c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
7068c3e34a4SDavid Howells 
7078c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
7088c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
7098c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
7108c3e34a4SDavid Howells 
7118c3e34a4SDavid Howells 		list_del_init(&call->link);
7128c3e34a4SDavid Howells 
7138c3e34a4SDavid Howells 		switch (atomic_read(&call->usage)) {
7148c3e34a4SDavid Howells 		case 0:
7158c3e34a4SDavid Howells 			ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
7168c3e34a4SDavid Howells 			break;
7178c3e34a4SDavid Howells 		case 1:
7188c3e34a4SDavid Howells 			if (del_timer_sync(&call->deadspan) != 0 &&
7198c3e34a4SDavid Howells 			    call->state != RXRPC_CALL_DEAD)
7208c3e34a4SDavid Howells 				rxrpc_dead_call_expired((unsigned long) call);
7218c3e34a4SDavid Howells 			if (call->state != RXRPC_CALL_DEAD)
7228c3e34a4SDavid Howells 				break;
7238c3e34a4SDavid Howells 		default:
7248c3e34a4SDavid Howells 			pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
7258c3e34a4SDavid Howells 			       call, atomic_read(&call->usage),
7268c3e34a4SDavid Howells 			       atomic_read(&call->ackr_not_idle),
7278c3e34a4SDavid Howells 			       rxrpc_call_states[call->state],
7288c3e34a4SDavid Howells 			       call->flags, call->events);
7298c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_queue))
7308c3e34a4SDavid Howells 				pr_err("Rx queue occupied\n");
7318c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_oos_queue))
7328c3e34a4SDavid Howells 				pr_err("OOS queue occupied\n");
7338c3e34a4SDavid Howells 			break;
7348c3e34a4SDavid Howells 		}
7358c3e34a4SDavid Howells 
7368c3e34a4SDavid Howells 		write_unlock_bh(&rxrpc_call_lock);
7378c3e34a4SDavid Howells 		cond_resched();
7388c3e34a4SDavid Howells 		write_lock_bh(&rxrpc_call_lock);
7398c3e34a4SDavid Howells 	}
7408c3e34a4SDavid Howells 
7418c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
7428c3e34a4SDavid Howells 	_leave("");
7438c3e34a4SDavid Howells }
7448c3e34a4SDavid Howells 
7458c3e34a4SDavid Howells /*
7468c3e34a4SDavid Howells  * handle call lifetime being exceeded
7478c3e34a4SDavid Howells  */
7488c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call)
7498c3e34a4SDavid Howells {
7508c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7518c3e34a4SDavid Howells 
7528c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7538c3e34a4SDavid Howells 		return;
7548c3e34a4SDavid Howells 
7558c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7568c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
7578c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
7588c3e34a4SDavid Howells 		set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
7598c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7608c3e34a4SDavid Howells 	}
7618c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
7628c3e34a4SDavid Howells }
7638c3e34a4SDavid Howells 
7648c3e34a4SDavid Howells /*
7658c3e34a4SDavid Howells  * handle resend timer expiry
7668c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
7678c3e34a4SDavid Howells  */
7688c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
7698c3e34a4SDavid Howells {
7708c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7718c3e34a4SDavid Howells 
7728c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7738c3e34a4SDavid Howells 
7748c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7758c3e34a4SDavid Howells 		return;
7768c3e34a4SDavid Howells 
7778c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
7788c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
7798c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7808c3e34a4SDavid Howells }
7818c3e34a4SDavid Howells 
7828c3e34a4SDavid Howells /*
7838c3e34a4SDavid Howells  * handle ACK timer expiry
7848c3e34a4SDavid Howells  */
7858c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
7868c3e34a4SDavid Howells {
7878c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7888c3e34a4SDavid Howells 
7898c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7908c3e34a4SDavid Howells 
7918c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7928c3e34a4SDavid Howells 		return;
7938c3e34a4SDavid Howells 
7948c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
7958c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
7968c3e34a4SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
7978c3e34a4SDavid Howells 		rxrpc_queue_call(call);
7988c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
7998c3e34a4SDavid Howells }
800