xref: /openbmc/linux/net/rxrpc/call_object.c (revision 8d94aa38)
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 const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
28999b69f8SDavid Howells 	[RXRPC_CALL_UNINITIALISED]		= "Uninit  ",
29999b69f8SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_CONN]		= "ClWtConn",
308c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_SEND_REQUEST]	= "ClSndReq",
318c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_REPLY]		= "ClAwtRpl",
328c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_RECV_REPLY]		= "ClRcvRpl",
338c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_FINAL_ACK]		= "ClFnlACK",
348c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SECURING]		= "SvSecure",
358c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACCEPTING]		= "SvAccept",
368c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_RECV_REQUEST]	= "SvRcvReq",
378c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACK_REQUEST]		= "SvAckReq",
388c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SEND_REPLY]		= "SvSndRpl",
398c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_AWAIT_ACK]		= "SvAwtACK",
408c3e34a4SDavid Howells 	[RXRPC_CALL_COMPLETE]			= "Complete",
41f5c17aaeSDavid Howells };
42f5c17aaeSDavid Howells 
43f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
44f5c17aaeSDavid Howells 	[RXRPC_CALL_SUCCEEDED]			= "Complete",
458c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_BUSY]		= "SvBusy  ",
468c3e34a4SDavid Howells 	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
478c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
48f5c17aaeSDavid Howells 	[RXRPC_CALL_LOCAL_ERROR]		= "LocError",
498c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
508c3e34a4SDavid Howells };
518c3e34a4SDavid Howells 
52fff72429SDavid Howells const char rxrpc_call_traces[rxrpc_call__nr_trace][4] = {
53fff72429SDavid Howells 	[rxrpc_call_new_client]		= "NWc",
54fff72429SDavid Howells 	[rxrpc_call_new_service]	= "NWs",
55fff72429SDavid Howells 	[rxrpc_call_queued]		= "QUE",
56fff72429SDavid Howells 	[rxrpc_call_queued_ref]		= "QUR",
57fff72429SDavid Howells 	[rxrpc_call_seen]		= "SEE",
58fff72429SDavid Howells 	[rxrpc_call_got]		= "GOT",
59fff72429SDavid Howells 	[rxrpc_call_got_skb]		= "Gsk",
60fff72429SDavid Howells 	[rxrpc_call_got_userid]		= "Gus",
61fff72429SDavid Howells 	[rxrpc_call_put]		= "PUT",
62fff72429SDavid Howells 	[rxrpc_call_put_skb]		= "Psk",
63fff72429SDavid Howells 	[rxrpc_call_put_userid]		= "Pus",
64fff72429SDavid Howells 	[rxrpc_call_put_noqueue]	= "PNQ",
65fff72429SDavid Howells };
66fff72429SDavid Howells 
678c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
688c3e34a4SDavid Howells LIST_HEAD(rxrpc_calls);
698c3e34a4SDavid Howells DEFINE_RWLOCK(rxrpc_call_lock);
708c3e34a4SDavid Howells 
718c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call);
728c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call);
738c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call);
748d94aa38SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call);
758c3e34a4SDavid Howells 
768c3e34a4SDavid Howells /*
778c3e34a4SDavid Howells  * find an extant server call
788c3e34a4SDavid Howells  * - called in process context with IRQs enabled
798c3e34a4SDavid Howells  */
808c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
818c3e34a4SDavid Howells 					      unsigned long user_call_ID)
828c3e34a4SDavid Howells {
838c3e34a4SDavid Howells 	struct rxrpc_call *call;
848c3e34a4SDavid Howells 	struct rb_node *p;
858c3e34a4SDavid Howells 
868c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
878c3e34a4SDavid Howells 
888c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
898c3e34a4SDavid Howells 
908c3e34a4SDavid Howells 	p = rx->calls.rb_node;
918c3e34a4SDavid Howells 	while (p) {
928c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
938c3e34a4SDavid Howells 
948c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
958c3e34a4SDavid Howells 			p = p->rb_left;
968c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
978c3e34a4SDavid Howells 			p = p->rb_right;
988c3e34a4SDavid Howells 		else
998c3e34a4SDavid Howells 			goto found_extant_call;
1008c3e34a4SDavid Howells 	}
1018c3e34a4SDavid Howells 
1028c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1038c3e34a4SDavid Howells 	_leave(" = NULL");
1048c3e34a4SDavid Howells 	return NULL;
1058c3e34a4SDavid Howells 
1068c3e34a4SDavid Howells found_extant_call:
107fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got);
1088c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1098c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
1108c3e34a4SDavid Howells 	return call;
1118c3e34a4SDavid Howells }
1128c3e34a4SDavid Howells 
1138c3e34a4SDavid Howells /*
1148c3e34a4SDavid Howells  * allocate a new call
1158c3e34a4SDavid Howells  */
1168c3e34a4SDavid Howells static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
1178c3e34a4SDavid Howells {
1188c3e34a4SDavid Howells 	struct rxrpc_call *call;
1198c3e34a4SDavid Howells 
1208c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1218c3e34a4SDavid Howells 	if (!call)
1228c3e34a4SDavid Howells 		return NULL;
1238c3e34a4SDavid Howells 
1248c3e34a4SDavid Howells 	call->acks_winsz = 16;
1258c3e34a4SDavid Howells 	call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
1268c3e34a4SDavid Howells 				    gfp);
1278c3e34a4SDavid Howells 	if (!call->acks_window) {
1288c3e34a4SDavid Howells 		kmem_cache_free(rxrpc_call_jar, call);
1298c3e34a4SDavid Howells 		return NULL;
1308c3e34a4SDavid Howells 	}
1318c3e34a4SDavid Howells 
1328c3e34a4SDavid Howells 	setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
1338c3e34a4SDavid Howells 		    (unsigned long) call);
1348c3e34a4SDavid Howells 	setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
1358c3e34a4SDavid Howells 		    (unsigned long) call);
1368c3e34a4SDavid Howells 	setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
1378c3e34a4SDavid Howells 		    (unsigned long) call);
1388c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
139999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
14045025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1418c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
1428c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_queue);
1438c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_oos_queue);
144d001648eSDavid Howells 	skb_queue_head_init(&call->knlrecv_queue);
14545025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1468c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
1478c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1488c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1498c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
1508c3e34a4SDavid Howells 
1518c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1528c3e34a4SDavid Howells 
1538c3e34a4SDavid Howells 	call->rx_data_expect = 1;
1548c3e34a4SDavid Howells 	call->rx_data_eaten = 0;
1558c3e34a4SDavid Howells 	call->rx_first_oos = 0;
1568c3e34a4SDavid Howells 	call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
1578c3e34a4SDavid Howells 	call->creation_jif = jiffies;
1588c3e34a4SDavid Howells 	return call;
1598c3e34a4SDavid Howells }
1608c3e34a4SDavid Howells 
1618c3e34a4SDavid Howells /*
162999b69f8SDavid Howells  * Allocate a new client call.
1638c3e34a4SDavid Howells  */
164aa390bbeSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
165999b69f8SDavid Howells 						  struct sockaddr_rxrpc *srx,
1668c3e34a4SDavid Howells 						  gfp_t gfp)
1678c3e34a4SDavid Howells {
1688c3e34a4SDavid Howells 	struct rxrpc_call *call;
1698c3e34a4SDavid Howells 
1708c3e34a4SDavid Howells 	_enter("");
1718c3e34a4SDavid Howells 
172999b69f8SDavid Howells 	ASSERT(rx->local != NULL);
1738c3e34a4SDavid Howells 
1748c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1758c3e34a4SDavid Howells 	if (!call)
1768c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
177999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
1788c3e34a4SDavid Howells 	call->rx_data_post = 1;
179999b69f8SDavid Howells 	call->service_id = srx->srx_service;
1808d94aa38SDavid Howells 	rcu_assign_pointer(call->socket, rx);
181999b69f8SDavid Howells 
182999b69f8SDavid Howells 	_leave(" = %p", call);
183999b69f8SDavid Howells 	return call;
184999b69f8SDavid Howells }
185999b69f8SDavid Howells 
186999b69f8SDavid Howells /*
187999b69f8SDavid Howells  * Begin client call.
188999b69f8SDavid Howells  */
189999b69f8SDavid Howells static int rxrpc_begin_client_call(struct rxrpc_call *call,
190999b69f8SDavid Howells 				   struct rxrpc_conn_parameters *cp,
191999b69f8SDavid Howells 				   struct sockaddr_rxrpc *srx,
192999b69f8SDavid Howells 				   gfp_t gfp)
193999b69f8SDavid Howells {
194999b69f8SDavid Howells 	int ret;
195999b69f8SDavid Howells 
196999b69f8SDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
197999b69f8SDavid Howells 	 * including channel number and call ID.
198999b69f8SDavid Howells 	 */
199aa390bbeSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
200999b69f8SDavid Howells 	if (ret < 0)
201999b69f8SDavid Howells 		return ret;
202999b69f8SDavid Howells 
20385f32278SDavid Howells 	spin_lock(&call->conn->params.peer->lock);
20485f32278SDavid Howells 	hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
20585f32278SDavid Howells 	spin_unlock(&call->conn->params.peer->lock);
2068c3e34a4SDavid Howells 
2078c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
2088c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
209999b69f8SDavid Howells 	return 0;
2108c3e34a4SDavid Howells }
2118c3e34a4SDavid Howells 
2128c3e34a4SDavid Howells /*
2138c3e34a4SDavid Howells  * set up a call for the given data
2148c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2158c3e34a4SDavid Howells  */
2168c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
21719ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
218999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2198c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2208c3e34a4SDavid Howells 					 gfp_t gfp)
2218c3e34a4SDavid Howells {
2228c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2238c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
224e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
225999b69f8SDavid Howells 	int ret;
2268c3e34a4SDavid Howells 
227999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2288c3e34a4SDavid Howells 
229aa390bbeSDavid Howells 	call = rxrpc_alloc_client_call(rx, srx, gfp);
2308c3e34a4SDavid Howells 	if (IS_ERR(call)) {
2318c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2328c3e34a4SDavid Howells 		return call;
2338c3e34a4SDavid Howells 	}
2348c3e34a4SDavid Howells 
2358d94aa38SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_new_client,
2368d94aa38SDavid Howells 			 atomic_read(&call->usage), 0,
2378d94aa38SDavid Howells 			 here, (const void *)user_call_ID);
238e34d4234SDavid Howells 
239999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2408c3e34a4SDavid Howells 	call->user_call_ID = user_call_ID;
2418c3e34a4SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
2428c3e34a4SDavid Howells 
2438c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2448c3e34a4SDavid Howells 
2458c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2468c3e34a4SDavid Howells 	parent = NULL;
2478c3e34a4SDavid Howells 	while (*pp) {
2488c3e34a4SDavid Howells 		parent = *pp;
2498c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2508c3e34a4SDavid Howells 
2518c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2528c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2538c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2548c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2558c3e34a4SDavid Howells 		else
2568c3e34a4SDavid Howells 			goto found_user_ID_now_present;
2578c3e34a4SDavid Howells 	}
2588c3e34a4SDavid Howells 
259fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got_userid);
2608c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2618c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
2628c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2638c3e34a4SDavid Howells 
2648c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
2658c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
2668c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
2678c3e34a4SDavid Howells 
268aa390bbeSDavid Howells 	ret = rxrpc_begin_client_call(call, cp, srx, gfp);
269999b69f8SDavid Howells 	if (ret < 0)
270999b69f8SDavid Howells 		goto error;
271999b69f8SDavid Howells 
2728c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2738c3e34a4SDavid Howells 
2748c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2758c3e34a4SDavid Howells 	return call;
2768c3e34a4SDavid Howells 
277999b69f8SDavid Howells error:
278999b69f8SDavid Howells 	write_lock(&rx->call_lock);
279999b69f8SDavid Howells 	rb_erase(&call->sock_node, &rx->calls);
280999b69f8SDavid Howells 	write_unlock(&rx->call_lock);
281fff72429SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put_userid);
282999b69f8SDavid Howells 
283999b69f8SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
284d1e858c5SDavid Howells 	list_del_init(&call->link);
285999b69f8SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
286999b69f8SDavid Howells 
2878d94aa38SDavid Howells error_out:
2888d94aa38SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
2898d94aa38SDavid Howells 				    RX_CALL_DEAD, ret);
29017b963e3SDavid Howells 	set_bit(RXRPC_CALL_RELEASED, &call->flags);
291fff72429SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
292999b69f8SDavid Howells 	_leave(" = %d", ret);
293999b69f8SDavid Howells 	return ERR_PTR(ret);
294999b69f8SDavid Howells 
2958c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2968c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2978c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2988c3e34a4SDavid Howells 	 * same time in different threads.
2998c3e34a4SDavid Howells 	 */
3008c3e34a4SDavid Howells found_user_ID_now_present:
3018c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
3028d94aa38SDavid Howells 	ret = -EEXIST;
3038d94aa38SDavid Howells 	goto error_out;
3048c3e34a4SDavid Howells }
3058c3e34a4SDavid Howells 
3068c3e34a4SDavid Howells /*
3078c3e34a4SDavid Howells  * set up an incoming call
3088c3e34a4SDavid Howells  * - called in process context with IRQs enabled
3098c3e34a4SDavid Howells  */
3108c3e34a4SDavid Howells struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
3118c3e34a4SDavid Howells 				       struct rxrpc_connection *conn,
31242886ffeSDavid Howells 				       struct sk_buff *skb)
3138c3e34a4SDavid Howells {
31442886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3158c3e34a4SDavid Howells 	struct rxrpc_call *call, *candidate;
316e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
317a1399f8bSDavid Howells 	u32 call_id, chan;
3188c3e34a4SDavid Howells 
3198c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
3208c3e34a4SDavid Howells 
3218c3e34a4SDavid Howells 	ASSERT(rx != NULL);
3228c3e34a4SDavid Howells 
3238c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
3248c3e34a4SDavid Howells 	if (!candidate)
3258c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
3268c3e34a4SDavid Howells 
327fff72429SDavid Howells 	trace_rxrpc_call(candidate, rxrpc_call_new_service,
328fff72429SDavid Howells 			 atomic_read(&candidate->usage), 0, here, NULL);
329e34d4234SDavid Howells 
330a1399f8bSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
3318c3e34a4SDavid Howells 	candidate->conn		= conn;
332df5d8bf7SDavid Howells 	candidate->peer		= conn->params.peer;
33342886ffeSDavid Howells 	candidate->cid		= sp->hdr.cid;
33442886ffeSDavid Howells 	candidate->call_id	= sp->hdr.callNumber;
335278ac0cdSDavid Howells 	candidate->security_ix	= sp->hdr.securityIndex;
3368c3e34a4SDavid Howells 	candidate->rx_data_post	= 0;
3378c3e34a4SDavid Howells 	candidate->state	= RXRPC_CALL_SERVER_ACCEPTING;
338dabe5a79SDavid Howells 	candidate->flags	|= (1 << RXRPC_CALL_IS_SERVICE);
3398c3e34a4SDavid Howells 	if (conn->security_ix > 0)
3408c3e34a4SDavid Howells 		candidate->state = RXRPC_CALL_SERVER_SECURING;
3418d94aa38SDavid Howells 	rcu_assign_pointer(candidate->socket, rx);
3428c3e34a4SDavid Howells 
343a1399f8bSDavid Howells 	spin_lock(&conn->channel_lock);
3448c3e34a4SDavid Howells 
3458c3e34a4SDavid Howells 	/* set the channel for this call */
346a1399f8bSDavid Howells 	call = rcu_dereference_protected(conn->channels[chan].call,
347a1399f8bSDavid Howells 					 lockdep_is_held(&conn->channel_lock));
348a1399f8bSDavid Howells 
34901a90a45SDavid Howells 	_debug("channel[%u] is %p", candidate->cid & RXRPC_CHANNELMASK, call);
35042886ffeSDavid Howells 	if (call && call->call_id == sp->hdr.callNumber) {
3518c3e34a4SDavid Howells 		/* already set; must've been a duplicate packet */
3528c3e34a4SDavid Howells 		_debug("extant call [%d]", call->state);
3538c3e34a4SDavid Howells 		ASSERTCMP(call->conn, ==, conn);
3548c3e34a4SDavid Howells 
3558c3e34a4SDavid Howells 		read_lock(&call->state_lock);
3568c3e34a4SDavid Howells 		switch (call->state) {
3578c3e34a4SDavid Howells 		case RXRPC_CALL_LOCALLY_ABORTED:
3588c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
3598c3e34a4SDavid Howells 				rxrpc_queue_call(call);
3608c3e34a4SDavid Howells 		case RXRPC_CALL_REMOTELY_ABORTED:
3618c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3628c3e34a4SDavid Howells 			goto aborted_call;
3638c3e34a4SDavid Howells 		default:
364fff72429SDavid Howells 			rxrpc_get_call(call, rxrpc_call_got);
3658c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
3668c3e34a4SDavid Howells 			goto extant_call;
3678c3e34a4SDavid Howells 		}
3688c3e34a4SDavid Howells 	}
3698c3e34a4SDavid Howells 
3708c3e34a4SDavid Howells 	if (call) {
3718c3e34a4SDavid Howells 		/* it seems the channel is still in use from the previous call
3728c3e34a4SDavid Howells 		 * - ditch the old binding if its call is now complete */
3738c3e34a4SDavid Howells 		_debug("CALL: %u { %s }",
3748c3e34a4SDavid Howells 		       call->debug_id, rxrpc_call_states[call->state]);
3758c3e34a4SDavid Howells 
376f5c17aaeSDavid Howells 		if (call->state == RXRPC_CALL_COMPLETE) {
37745025bceSDavid Howells 			__rxrpc_disconnect_call(conn, call);
3788c3e34a4SDavid Howells 		} else {
379a1399f8bSDavid Howells 			spin_unlock(&conn->channel_lock);
3808c3e34a4SDavid Howells 			kmem_cache_free(rxrpc_call_jar, candidate);
3818c3e34a4SDavid Howells 			_leave(" = -EBUSY");
3828c3e34a4SDavid Howells 			return ERR_PTR(-EBUSY);
3838c3e34a4SDavid Howells 		}
3848c3e34a4SDavid Howells 	}
3858c3e34a4SDavid Howells 
3868c3e34a4SDavid Howells 	/* check the call number isn't duplicate */
3878c3e34a4SDavid Howells 	_debug("check dup");
38842886ffeSDavid Howells 	call_id = sp->hdr.callNumber;
3898c3e34a4SDavid Howells 
390a1399f8bSDavid Howells 	/* We just ignore calls prior to the current call ID.  Terminated calls
391a1399f8bSDavid Howells 	 * are handled via the connection.
3928c3e34a4SDavid Howells 	 */
393a1399f8bSDavid Howells 	if (call_id <= conn->channels[chan].call_counter)
394a1399f8bSDavid Howells 		goto old_call; /* TODO: Just drop packet */
3958c3e34a4SDavid Howells 
3968c3e34a4SDavid Howells 	/* make the call available */
3978c3e34a4SDavid Howells 	_debug("new call");
3988c3e34a4SDavid Howells 	call = candidate;
3998c3e34a4SDavid Howells 	candidate = NULL;
400a1399f8bSDavid Howells 	conn->channels[chan].call_counter = call_id;
401a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
4025627cc8bSDavid Howells 	rxrpc_get_connection(conn);
403df5d8bf7SDavid Howells 	rxrpc_get_peer(call->peer);
404a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4058c3e34a4SDavid Howells 
40685f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
40785f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
40885f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
4098c3e34a4SDavid Howells 
4108c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
4118c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
4128c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
4138c3e34a4SDavid Howells 
41419ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
4158c3e34a4SDavid Howells 
4168c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
4178c3e34a4SDavid Howells 
4188c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
4198c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
4208c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
4218c3e34a4SDavid Howells 	return call;
4228c3e34a4SDavid Howells 
4238c3e34a4SDavid Howells extant_call:
424a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4258c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4268c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
4278c3e34a4SDavid Howells 	return call;
4288c3e34a4SDavid Howells 
4298c3e34a4SDavid Howells aborted_call:
430a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4318c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4328c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
4338c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
4348c3e34a4SDavid Howells 
4358c3e34a4SDavid Howells old_call:
436a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
4378c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
4388c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
4398c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
4408c3e34a4SDavid Howells }
4418c3e34a4SDavid Howells 
4428c3e34a4SDavid Howells /*
4438d94aa38SDavid Howells  * Queue a call's work processor, getting a ref to pass to the work queue.
4448d94aa38SDavid Howells  */
4458d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call)
4468d94aa38SDavid Howells {
4478d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
4488d94aa38SDavid Howells 	int n = __atomic_add_unless(&call->usage, 1, 0);
4498d94aa38SDavid Howells 	int m = atomic_read(&call->skb_count);
4508d94aa38SDavid Howells 	if (n == 0)
4518d94aa38SDavid Howells 		return false;
4528d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
4538d94aa38SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued, n + 1, m, here, NULL);
4548d94aa38SDavid Howells 	else
4558d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4568d94aa38SDavid Howells 	return true;
4578d94aa38SDavid Howells }
4588d94aa38SDavid Howells 
4598d94aa38SDavid Howells /*
4608d94aa38SDavid Howells  * Queue a call's work processor, passing the callers ref to the work queue.
4618d94aa38SDavid Howells  */
4628d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call)
4638d94aa38SDavid Howells {
4648d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
4658d94aa38SDavid Howells 	int n = atomic_read(&call->usage);
4668d94aa38SDavid Howells 	int m = atomic_read(&call->skb_count);
4678d94aa38SDavid Howells 	ASSERTCMP(n, >=, 1);
4688d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
4698d94aa38SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued_ref, n, m, here, NULL);
4708d94aa38SDavid Howells 	else
4718d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4728d94aa38SDavid Howells 	return true;
4738d94aa38SDavid Howells }
4748d94aa38SDavid Howells 
4758d94aa38SDavid Howells /*
476e34d4234SDavid Howells  * Note the re-emergence of a call.
477e34d4234SDavid Howells  */
478e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call)
479e34d4234SDavid Howells {
480e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
481e34d4234SDavid Howells 	if (call) {
482e34d4234SDavid Howells 		int n = atomic_read(&call->usage);
483e34d4234SDavid Howells 		int m = atomic_read(&call->skb_count);
484e34d4234SDavid Howells 
485fff72429SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_seen, n, m, here, NULL);
486e34d4234SDavid Howells 	}
487e34d4234SDavid Howells }
488e34d4234SDavid Howells 
489e34d4234SDavid Howells /*
490e34d4234SDavid Howells  * Note the addition of a ref on a call.
491e34d4234SDavid Howells  */
492fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
493e34d4234SDavid Howells {
494e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
495e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
496e34d4234SDavid Howells 	int m = atomic_read(&call->skb_count);
497e34d4234SDavid Howells 
498fff72429SDavid Howells 	trace_rxrpc_call(call, op, n, m, here, NULL);
499e34d4234SDavid Howells }
500e34d4234SDavid Howells 
501e34d4234SDavid Howells /*
502e34d4234SDavid Howells  * Note the addition of a ref on a call for a socket buffer.
503e34d4234SDavid Howells  */
504e34d4234SDavid Howells void rxrpc_get_call_for_skb(struct rxrpc_call *call, struct sk_buff *skb)
505e34d4234SDavid Howells {
506e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
507e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
508e34d4234SDavid Howells 	int m = atomic_inc_return(&call->skb_count);
509e34d4234SDavid Howells 
510fff72429SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_got_skb, n, m, here, skb);
511e34d4234SDavid Howells }
512e34d4234SDavid Howells 
513e34d4234SDavid Howells /*
5148c3e34a4SDavid Howells  * detach a call from a socket and set up for release
5158c3e34a4SDavid Howells  */
5168d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
5178c3e34a4SDavid Howells {
5188c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
5198c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
5208c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
5218c3e34a4SDavid Howells 	       call->rx_first_oos);
5228c3e34a4SDavid Howells 
523e34d4234SDavid Howells 	rxrpc_see_call(call);
524e34d4234SDavid Howells 
5258c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
5268c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
5278c3e34a4SDavid Howells 		BUG();
5288c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
5298c3e34a4SDavid Howells 
5308c3e34a4SDavid Howells 	/* dissociate from the socket
5318c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
5328c3e34a4SDavid Howells 	 */
5338d94aa38SDavid Howells 	_debug("RELEASE CALL %p (%d)", call, call->debug_id);
5348c3e34a4SDavid Howells 
535f4fdb352SDavid Howells 	if (call->peer) {
536f4fdb352SDavid Howells 		spin_lock(&call->peer->lock);
537e653cfe4SDavid Howells 		hlist_del_init(&call->error_link);
538f4fdb352SDavid Howells 		spin_unlock(&call->peer->lock);
539f4fdb352SDavid Howells 	}
540e653cfe4SDavid Howells 
5418c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
5428c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
5438c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
5448c3e34a4SDavid Howells 		       call, call->events, call->flags);
5458c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
5468c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
5478c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
5488c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
5498c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
5508c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
5518c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
5528d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_userid);
5538c3e34a4SDavid Howells 	}
5548c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
5558c3e34a4SDavid Howells 
5568c3e34a4SDavid Howells 	/* free up the channel for reuse */
5578d94aa38SDavid Howells 	if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK) {
5588d94aa38SDavid Howells 		clear_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
5598d94aa38SDavid Howells 		rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
5608d94aa38SDavid Howells 		rxrpc_call_completed(call);
5618d94aa38SDavid Howells 	} else {
562a1399f8bSDavid Howells 		write_lock_bh(&call->state_lock);
5638c3e34a4SDavid Howells 
5648d94aa38SDavid Howells 		if (call->state < RXRPC_CALL_COMPLETE) {
5658c3e34a4SDavid Howells 			_debug("+++ ABORTING STATE %d +++\n", call->state);
566f5c17aaeSDavid Howells 			__rxrpc_abort_call(call, RX_CALL_DEAD, ECONNRESET);
5678d94aa38SDavid Howells 			clear_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
5688d94aa38SDavid Howells 			rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
5698c3e34a4SDavid Howells 		}
5708c3e34a4SDavid Howells 
5718d94aa38SDavid Howells 		write_unlock_bh(&call->state_lock);
5728d94aa38SDavid Howells 	}
5738d94aa38SDavid Howells 
5748d94aa38SDavid Howells 	if (call->conn)
575e653cfe4SDavid Howells 		rxrpc_disconnect_call(call);
576e653cfe4SDavid Howells 
5778c3e34a4SDavid Howells 	/* clean up the Rx queue */
5788c3e34a4SDavid Howells 	if (!skb_queue_empty(&call->rx_queue) ||
5798c3e34a4SDavid Howells 	    !skb_queue_empty(&call->rx_oos_queue)) {
5808c3e34a4SDavid Howells 		struct rxrpc_skb_priv *sp;
5818c3e34a4SDavid Howells 		struct sk_buff *skb;
5828c3e34a4SDavid Howells 
5838c3e34a4SDavid Howells 		_debug("purge Rx queues");
5848c3e34a4SDavid Howells 
5858c3e34a4SDavid Howells 		spin_lock_bh(&call->lock);
5868c3e34a4SDavid Howells 		while ((skb = skb_dequeue(&call->rx_queue)) ||
5878c3e34a4SDavid Howells 		       (skb = skb_dequeue(&call->rx_oos_queue))) {
5888c3e34a4SDavid Howells 			spin_unlock_bh(&call->lock);
5898c3e34a4SDavid Howells 
59055cae7a4SArnd Bergmann 			sp = rxrpc_skb(skb);
5918c3e34a4SDavid Howells 			_debug("- zap %s %%%u #%u",
5928c3e34a4SDavid Howells 			       rxrpc_pkts[sp->hdr.type],
5938c3e34a4SDavid Howells 			       sp->hdr.serial, sp->hdr.seq);
5948c3e34a4SDavid Howells 			rxrpc_free_skb(skb);
5958c3e34a4SDavid Howells 			spin_lock_bh(&call->lock);
5968c3e34a4SDavid Howells 		}
5978c3e34a4SDavid Howells 		spin_unlock_bh(&call->lock);
5988c3e34a4SDavid Howells 	}
5998d94aa38SDavid Howells 	rxrpc_purge_queue(&call->knlrecv_queue);
6008c3e34a4SDavid Howells 
6018c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
6028c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
6038c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
6048c3e34a4SDavid Howells 
6058c3e34a4SDavid Howells 	_leave("");
6068c3e34a4SDavid Howells }
6078c3e34a4SDavid Howells 
6088c3e34a4SDavid Howells /*
6098c3e34a4SDavid Howells  * release all the calls associated with a socket
6108c3e34a4SDavid Howells  */
6118c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
6128c3e34a4SDavid Howells {
6138c3e34a4SDavid Howells 	struct rxrpc_call *call;
6148c3e34a4SDavid Howells 	struct rb_node *p;
6158c3e34a4SDavid Howells 
6168c3e34a4SDavid Howells 	_enter("%p", rx);
6178c3e34a4SDavid Howells 
6188c3e34a4SDavid Howells 	read_lock_bh(&rx->call_lock);
6198c3e34a4SDavid Howells 
6208c3e34a4SDavid Howells 	/* kill the not-yet-accepted incoming calls */
6218c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->secureq, accept_link) {
6228d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
6238c3e34a4SDavid Howells 	}
6248c3e34a4SDavid Howells 
6258c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->acceptq, accept_link) {
6268d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
6278c3e34a4SDavid Howells 	}
6288c3e34a4SDavid Howells 
629f36b5e44SDavid Howells 	/* mark all the calls as no longer wanting incoming packets */
630f36b5e44SDavid Howells 	for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
631f36b5e44SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
6328d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
633f36b5e44SDavid Howells 	}
634f36b5e44SDavid Howells 
6358c3e34a4SDavid Howells 	read_unlock_bh(&rx->call_lock);
6368c3e34a4SDavid Howells 	_leave("");
6378c3e34a4SDavid Howells }
6388c3e34a4SDavid Howells 
6398c3e34a4SDavid Howells /*
6408c3e34a4SDavid Howells  * release a call
6418c3e34a4SDavid Howells  */
642fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
6438c3e34a4SDavid Howells {
644e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
645e34d4234SDavid Howells 	int n, m;
646e34d4234SDavid Howells 
6478c3e34a4SDavid Howells 	ASSERT(call != NULL);
6488c3e34a4SDavid Howells 
649e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
650e34d4234SDavid Howells 	m = atomic_read(&call->skb_count);
651fff72429SDavid Howells 	trace_rxrpc_call(call, op, n, m, here, NULL);
652e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
653e34d4234SDavid Howells 	if (n == 0) {
6548c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
655e34d4234SDavid Howells 		WARN_ON(m != 0);
6568d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
6578c3e34a4SDavid Howells 	}
658e34d4234SDavid Howells }
659e34d4234SDavid Howells 
660e34d4234SDavid Howells /*
661e34d4234SDavid Howells  * Release a call ref held by a socket buffer.
662e34d4234SDavid Howells  */
663e34d4234SDavid Howells void rxrpc_put_call_for_skb(struct rxrpc_call *call, struct sk_buff *skb)
664e34d4234SDavid Howells {
665e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
666e34d4234SDavid Howells 	int n, m;
667e34d4234SDavid Howells 
668e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
669e34d4234SDavid Howells 	m = atomic_dec_return(&call->skb_count);
670fff72429SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_put_skb, n, m, here, skb);
671e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
672e34d4234SDavid Howells 	if (n == 0) {
673e34d4234SDavid Howells 		_debug("call %d dead", call->debug_id);
674e34d4234SDavid Howells 		WARN_ON(m != 0);
6758d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
676e34d4234SDavid Howells 	}
6778c3e34a4SDavid Howells }
6788c3e34a4SDavid Howells 
6798c3e34a4SDavid Howells /*
680dee46364SDavid Howells  * Final call destruction under RCU.
681dee46364SDavid Howells  */
682dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
683dee46364SDavid Howells {
684dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
685dee46364SDavid Howells 
686dee46364SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
687d001648eSDavid Howells 	rxrpc_purge_queue(&call->knlrecv_queue);
688df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
689dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
690dee46364SDavid Howells }
691dee46364SDavid Howells 
692dee46364SDavid Howells /*
6938c3e34a4SDavid Howells  * clean up a call
6948c3e34a4SDavid Howells  */
6958c3e34a4SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call)
6968c3e34a4SDavid Howells {
6978c3e34a4SDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
6988c3e34a4SDavid Howells 
6998d94aa38SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
7008d94aa38SDavid Howells 	list_del_init(&call->link);
7018d94aa38SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
7028c3e34a4SDavid Howells 
7038c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
7048c3e34a4SDavid Howells 
7058c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
7068c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
7078c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
7088c3e34a4SDavid Howells 
7098d94aa38SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
7108c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
7118d94aa38SDavid Howells 	ASSERT(!work_pending(&call->processor));
712e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
7138c3e34a4SDavid Howells 
7148c3e34a4SDavid Howells 	if (call->acks_window) {
7158c3e34a4SDavid Howells 		_debug("kill Tx window %d",
7168c3e34a4SDavid Howells 		       CIRC_CNT(call->acks_head, call->acks_tail,
7178c3e34a4SDavid Howells 				call->acks_winsz));
7188c3e34a4SDavid Howells 		smp_mb();
7198c3e34a4SDavid Howells 		while (CIRC_CNT(call->acks_head, call->acks_tail,
7208c3e34a4SDavid Howells 				call->acks_winsz) > 0) {
7218c3e34a4SDavid Howells 			struct rxrpc_skb_priv *sp;
7228c3e34a4SDavid Howells 			unsigned long _skb;
7238c3e34a4SDavid Howells 
7248c3e34a4SDavid Howells 			_skb = call->acks_window[call->acks_tail] & ~1;
7258c3e34a4SDavid Howells 			sp = rxrpc_skb((struct sk_buff *)_skb);
7268c3e34a4SDavid Howells 			_debug("+++ clear Tx %u", sp->hdr.seq);
7278c3e34a4SDavid Howells 			rxrpc_free_skb((struct sk_buff *)_skb);
7288c3e34a4SDavid Howells 			call->acks_tail =
7298c3e34a4SDavid Howells 				(call->acks_tail + 1) & (call->acks_winsz - 1);
7308c3e34a4SDavid Howells 		}
7318c3e34a4SDavid Howells 
7328c3e34a4SDavid Howells 		kfree(call->acks_window);
7338c3e34a4SDavid Howells 	}
7348c3e34a4SDavid Howells 
7358c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
7368c3e34a4SDavid Howells 
7378c3e34a4SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
7388c3e34a4SDavid Howells 	ASSERT(skb_queue_empty(&call->rx_oos_queue));
739d001648eSDavid Howells 	rxrpc_purge_queue(&call->knlrecv_queue);
740dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
7418c3e34a4SDavid Howells }
7428c3e34a4SDavid Howells 
7438c3e34a4SDavid Howells /*
7448d94aa38SDavid Howells  * Make sure that all calls are gone.
7458c3e34a4SDavid Howells  */
7468c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
7478c3e34a4SDavid Howells {
7488c3e34a4SDavid Howells 	struct rxrpc_call *call;
7498c3e34a4SDavid Howells 
7508c3e34a4SDavid Howells 	_enter("");
7518d94aa38SDavid Howells 
7528d94aa38SDavid Howells 	if (list_empty(&rxrpc_calls))
7538d94aa38SDavid Howells 		return;
7548d94aa38SDavid Howells 
7558c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
7568c3e34a4SDavid Howells 
7578c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
7588c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
7598c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
7608c3e34a4SDavid Howells 
761e34d4234SDavid Howells 		rxrpc_see_call(call);
7628c3e34a4SDavid Howells 		list_del_init(&call->link);
7638c3e34a4SDavid Howells 
7648c3e34a4SDavid Howells 		pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
7658c3e34a4SDavid Howells 		       call, atomic_read(&call->usage),
7668c3e34a4SDavid Howells 		       atomic_read(&call->ackr_not_idle),
7678c3e34a4SDavid Howells 		       rxrpc_call_states[call->state],
7688c3e34a4SDavid Howells 		       call->flags, call->events);
7698c3e34a4SDavid Howells 		if (!skb_queue_empty(&call->rx_queue))
7708c3e34a4SDavid Howells 			pr_err("Rx queue occupied\n");
7718c3e34a4SDavid Howells 		if (!skb_queue_empty(&call->rx_oos_queue))
7728c3e34a4SDavid Howells 			pr_err("OOS queue occupied\n");
7738c3e34a4SDavid Howells 
7748c3e34a4SDavid Howells 		write_unlock_bh(&rxrpc_call_lock);
7758c3e34a4SDavid Howells 		cond_resched();
7768c3e34a4SDavid Howells 		write_lock_bh(&rxrpc_call_lock);
7778c3e34a4SDavid Howells 	}
7788c3e34a4SDavid Howells 
7798c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
7808c3e34a4SDavid Howells 	_leave("");
7818c3e34a4SDavid Howells }
7828c3e34a4SDavid Howells 
7838c3e34a4SDavid Howells /*
7848c3e34a4SDavid Howells  * handle call lifetime being exceeded
7858c3e34a4SDavid Howells  */
7868c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call)
7878c3e34a4SDavid Howells {
7888c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7898c3e34a4SDavid Howells 
790f5c17aaeSDavid Howells 	_enter("{%d}", call->debug_id);
791f5c17aaeSDavid Howells 
792e34d4234SDavid Howells 	rxrpc_see_call(call);
7938c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
7948c3e34a4SDavid Howells 		return;
7958c3e34a4SDavid Howells 
7968c3e34a4SDavid Howells 	set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
7978c3e34a4SDavid Howells 	rxrpc_queue_call(call);
7988c3e34a4SDavid Howells }
7998c3e34a4SDavid Howells 
8008c3e34a4SDavid Howells /*
8018c3e34a4SDavid Howells  * handle resend timer expiry
8028c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
8038c3e34a4SDavid Howells  */
8048c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
8058c3e34a4SDavid Howells {
8068c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
8078c3e34a4SDavid Howells 
8088c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
8098c3e34a4SDavid Howells 
810e34d4234SDavid Howells 	rxrpc_see_call(call);
8118c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
8128c3e34a4SDavid Howells 		return;
8138c3e34a4SDavid Howells 
8148c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
8158c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
8168c3e34a4SDavid Howells 		rxrpc_queue_call(call);
8178c3e34a4SDavid Howells }
8188c3e34a4SDavid Howells 
8198c3e34a4SDavid Howells /*
8208c3e34a4SDavid Howells  * handle ACK timer expiry
8218c3e34a4SDavid Howells  */
8228c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
8238c3e34a4SDavid Howells {
8248c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
8258c3e34a4SDavid Howells 
8268c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
8278c3e34a4SDavid Howells 
828e34d4234SDavid Howells 	rxrpc_see_call(call);
8298c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
8308c3e34a4SDavid Howells 		return;
8318c3e34a4SDavid Howells 
832f5c17aaeSDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
8338c3e34a4SDavid Howells 		rxrpc_queue_call(call);
8348c3e34a4SDavid Howells }
835