xref: /openbmc/linux/net/rxrpc/call_object.c (revision 357f5ef6)
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",
3300e90712SDavid Howells 	[RXRPC_CALL_SERVER_PREALLOC]		= "SvPrealc",
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_REMOTELY_ABORTED]		= "RmtAbort",
468c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
47f5c17aaeSDavid Howells 	[RXRPC_CALL_LOCAL_ERROR]		= "LocError",
488c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
498c3e34a4SDavid Howells };
508c3e34a4SDavid Howells 
51fff72429SDavid Howells const char rxrpc_call_traces[rxrpc_call__nr_trace][4] = {
52fff72429SDavid Howells 	[rxrpc_call_new_client]		= "NWc",
53fff72429SDavid Howells 	[rxrpc_call_new_service]	= "NWs",
54fff72429SDavid Howells 	[rxrpc_call_queued]		= "QUE",
55fff72429SDavid Howells 	[rxrpc_call_queued_ref]		= "QUR",
56fff72429SDavid Howells 	[rxrpc_call_seen]		= "SEE",
57fff72429SDavid Howells 	[rxrpc_call_got]		= "GOT",
58fff72429SDavid Howells 	[rxrpc_call_got_userid]		= "Gus",
59cbd00891SDavid Howells 	[rxrpc_call_got_kernel]		= "Gke",
60fff72429SDavid Howells 	[rxrpc_call_put]		= "PUT",
61fff72429SDavid Howells 	[rxrpc_call_put_userid]		= "Pus",
62cbd00891SDavid Howells 	[rxrpc_call_put_kernel]		= "Pke",
63fff72429SDavid Howells 	[rxrpc_call_put_noqueue]	= "PNQ",
64fff72429SDavid Howells };
65fff72429SDavid Howells 
668c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
678c3e34a4SDavid Howells LIST_HEAD(rxrpc_calls);
688c3e34a4SDavid Howells DEFINE_RWLOCK(rxrpc_call_lock);
698c3e34a4SDavid Howells 
70248f219cSDavid Howells static void rxrpc_call_timer_expired(unsigned long _call)
71248f219cSDavid Howells {
72248f219cSDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *)_call;
73248f219cSDavid Howells 
74248f219cSDavid Howells 	_enter("%d", call->debug_id);
75248f219cSDavid Howells 
76248f219cSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE)
77248f219cSDavid Howells 		rxrpc_queue_call(call);
78248f219cSDavid Howells }
798c3e34a4SDavid Howells 
808c3e34a4SDavid Howells /*
818c3e34a4SDavid Howells  * find an extant server call
828c3e34a4SDavid Howells  * - called in process context with IRQs enabled
838c3e34a4SDavid Howells  */
848c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
858c3e34a4SDavid Howells 					      unsigned long user_call_ID)
868c3e34a4SDavid Howells {
878c3e34a4SDavid Howells 	struct rxrpc_call *call;
888c3e34a4SDavid Howells 	struct rb_node *p;
898c3e34a4SDavid Howells 
908c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
918c3e34a4SDavid Howells 
928c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
938c3e34a4SDavid Howells 
948c3e34a4SDavid Howells 	p = rx->calls.rb_node;
958c3e34a4SDavid Howells 	while (p) {
968c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
978c3e34a4SDavid Howells 
988c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
998c3e34a4SDavid Howells 			p = p->rb_left;
1008c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
1018c3e34a4SDavid Howells 			p = p->rb_right;
1028c3e34a4SDavid Howells 		else
1038c3e34a4SDavid Howells 			goto found_extant_call;
1048c3e34a4SDavid Howells 	}
1058c3e34a4SDavid Howells 
1068c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1078c3e34a4SDavid Howells 	_leave(" = NULL");
1088c3e34a4SDavid Howells 	return NULL;
1098c3e34a4SDavid Howells 
1108c3e34a4SDavid Howells found_extant_call:
111fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got);
1128c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1138c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
1148c3e34a4SDavid Howells 	return call;
1158c3e34a4SDavid Howells }
1168c3e34a4SDavid Howells 
1178c3e34a4SDavid Howells /*
1188c3e34a4SDavid Howells  * allocate a new call
1198c3e34a4SDavid Howells  */
12000e90712SDavid Howells struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
1218c3e34a4SDavid Howells {
1228c3e34a4SDavid Howells 	struct rxrpc_call *call;
1238c3e34a4SDavid Howells 
1248c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1258c3e34a4SDavid Howells 	if (!call)
1268c3e34a4SDavid Howells 		return NULL;
1278c3e34a4SDavid Howells 
128248f219cSDavid Howells 	call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
129248f219cSDavid Howells 				    sizeof(struct sk_buff *),
1308c3e34a4SDavid Howells 				    gfp);
131248f219cSDavid Howells 	if (!call->rxtx_buffer)
132248f219cSDavid Howells 		goto nomem;
1338c3e34a4SDavid Howells 
134248f219cSDavid Howells 	call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
135248f219cSDavid Howells 	if (!call->rxtx_annotations)
136248f219cSDavid Howells 		goto nomem_2;
137248f219cSDavid Howells 
138248f219cSDavid Howells 	setup_timer(&call->timer, rxrpc_call_timer_expired,
1398c3e34a4SDavid Howells 		    (unsigned long)call);
1408c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
141999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
14245025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1438c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
144248f219cSDavid Howells 	INIT_LIST_HEAD(&call->recvmsg_link);
145248f219cSDavid Howells 	INIT_LIST_HEAD(&call->sock_link);
14645025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1478c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
1488c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1498c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1508c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
1518c3e34a4SDavid Howells 
1528c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1538c3e34a4SDavid Howells 
154248f219cSDavid Howells 	/* Leave space in the ring to handle a maxed-out jumbo packet */
15575e42126SDavid Howells 	call->rx_winsize = rxrpc_rx_window_size;
156248f219cSDavid Howells 	call->tx_winsize = 16;
157248f219cSDavid Howells 	call->rx_expect_next = 1;
1588c3e34a4SDavid Howells 	return call;
159248f219cSDavid Howells 
160248f219cSDavid Howells nomem_2:
161248f219cSDavid Howells 	kfree(call->rxtx_buffer);
162248f219cSDavid Howells nomem:
163248f219cSDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
164248f219cSDavid Howells 	return NULL;
1658c3e34a4SDavid Howells }
1668c3e34a4SDavid Howells 
1678c3e34a4SDavid Howells /*
168999b69f8SDavid Howells  * Allocate a new client call.
1698c3e34a4SDavid Howells  */
170248f219cSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct sockaddr_rxrpc *srx,
1718c3e34a4SDavid Howells 						  gfp_t gfp)
1728c3e34a4SDavid Howells {
1738c3e34a4SDavid Howells 	struct rxrpc_call *call;
1748c3e34a4SDavid Howells 
1758c3e34a4SDavid Howells 	_enter("");
1768c3e34a4SDavid Howells 
1778c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1788c3e34a4SDavid Howells 	if (!call)
1798c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
180999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
181999b69f8SDavid Howells 	call->service_id = srx->srx_service;
182999b69f8SDavid Howells 
183999b69f8SDavid Howells 	_leave(" = %p", call);
184999b69f8SDavid Howells 	return call;
185999b69f8SDavid Howells }
186999b69f8SDavid Howells 
187999b69f8SDavid Howells /*
188248f219cSDavid Howells  * Initiate the call ack/resend/expiry timer.
189999b69f8SDavid Howells  */
190248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call)
191999b69f8SDavid Howells {
192248f219cSDavid Howells 	unsigned long expire_at;
193999b69f8SDavid Howells 
194248f219cSDavid Howells 	expire_at = jiffies + rxrpc_max_call_lifetime;
195248f219cSDavid Howells 	call->expire_at = expire_at;
196248f219cSDavid Howells 	call->ack_at = expire_at;
197248f219cSDavid Howells 	call->resend_at = expire_at;
198248f219cSDavid Howells 	call->timer.expires = expire_at;
199248f219cSDavid Howells 	add_timer(&call->timer);
2008c3e34a4SDavid Howells }
2018c3e34a4SDavid Howells 
2028c3e34a4SDavid Howells /*
2038c3e34a4SDavid Howells  * set up a call for the given data
2048c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2058c3e34a4SDavid Howells  */
2068c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
20719ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
208999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2098c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2108c3e34a4SDavid Howells 					 gfp_t gfp)
2118c3e34a4SDavid Howells {
2128c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2138c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
214e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
215999b69f8SDavid Howells 	int ret;
2168c3e34a4SDavid Howells 
217999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2188c3e34a4SDavid Howells 
219248f219cSDavid Howells 	call = rxrpc_alloc_client_call(srx, gfp);
2208c3e34a4SDavid Howells 	if (IS_ERR(call)) {
2218c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2228c3e34a4SDavid Howells 		return call;
2238c3e34a4SDavid Howells 	}
2248c3e34a4SDavid Howells 
2252ab27215SDavid Howells 	trace_rxrpc_call(call, 0, atomic_read(&call->usage), here,
2262ab27215SDavid Howells 			 (const void *)user_call_ID);
227e34d4234SDavid Howells 
228999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2298c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2308c3e34a4SDavid Howells 
2318c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2328c3e34a4SDavid Howells 	parent = NULL;
2338c3e34a4SDavid Howells 	while (*pp) {
2348c3e34a4SDavid Howells 		parent = *pp;
2358c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2368c3e34a4SDavid Howells 
2378c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2388c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2398c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2408c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2418c3e34a4SDavid Howells 		else
242357f5ef6SDavid Howells 			goto error_dup_user_ID;
2438c3e34a4SDavid Howells 	}
2448c3e34a4SDavid Howells 
245248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
246357f5ef6SDavid Howells 	call->user_call_ID = user_call_ID;
247357f5ef6SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
248fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got_userid);
2498c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2508c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
251248f219cSDavid Howells 	list_add(&call->sock_link, &rx->sock_calls);
252248f219cSDavid Howells 
2538c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2548c3e34a4SDavid Howells 
255248f219cSDavid Howells 	write_lock(&rxrpc_call_lock);
2568c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
257248f219cSDavid Howells 	write_unlock(&rxrpc_call_lock);
2588c3e34a4SDavid Howells 
259248f219cSDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
260248f219cSDavid Howells 	 * including channel number and call ID.
261248f219cSDavid Howells 	 */
262248f219cSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
263999b69f8SDavid Howells 	if (ret < 0)
264999b69f8SDavid Howells 		goto error;
265999b69f8SDavid Howells 
266248f219cSDavid Howells 	spin_lock_bh(&call->conn->params.peer->lock);
267248f219cSDavid Howells 	hlist_add_head(&call->error_link,
268248f219cSDavid Howells 		       &call->conn->params.peer->error_targets);
269248f219cSDavid Howells 	spin_unlock_bh(&call->conn->params.peer->lock);
270248f219cSDavid Howells 
271248f219cSDavid Howells 	rxrpc_start_call_timer(call);
272248f219cSDavid Howells 
2738c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2748c3e34a4SDavid Howells 
2758c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2768c3e34a4SDavid Howells 	return call;
2778c3e34a4SDavid Howells 
2788c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2798c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2808c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2818c3e34a4SDavid Howells 	 * same time in different threads.
2828c3e34a4SDavid Howells 	 */
283357f5ef6SDavid Howells error_dup_user_ID:
2848c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2858d94aa38SDavid Howells 	ret = -EEXIST;
286357f5ef6SDavid Howells 
287357f5ef6SDavid Howells error:
288357f5ef6SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
289357f5ef6SDavid Howells 				    RX_CALL_DEAD, ret);
290357f5ef6SDavid Howells 	rxrpc_release_call(rx, call);
291357f5ef6SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
292357f5ef6SDavid Howells 	_leave(" = %d", ret);
293357f5ef6SDavid Howells 	return ERR_PTR(ret);
2948c3e34a4SDavid Howells }
2958c3e34a4SDavid Howells 
2968c3e34a4SDavid Howells /*
297248f219cSDavid Howells  * Set up an incoming call.  call->conn points to the connection.
298248f219cSDavid Howells  * This is called in BH context and isn't allowed to fail.
2998c3e34a4SDavid Howells  */
300248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx,
301248f219cSDavid Howells 			 struct rxrpc_call *call,
30242886ffeSDavid Howells 			 struct sk_buff *skb)
3038c3e34a4SDavid Howells {
304248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
30542886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
306248f219cSDavid Howells 	u32 chan;
3078c3e34a4SDavid Howells 
308248f219cSDavid Howells 	_enter(",%d", call->conn->debug_id);
3098c3e34a4SDavid Howells 
310248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
311248f219cSDavid Howells 	call->call_id		= sp->hdr.callNumber;
312248f219cSDavid Howells 	call->service_id	= sp->hdr.serviceId;
313248f219cSDavid Howells 	call->cid		= sp->hdr.cid;
314248f219cSDavid Howells 	call->state		= RXRPC_CALL_SERVER_ACCEPTING;
315248f219cSDavid Howells 	if (sp->hdr.securityIndex > 0)
316248f219cSDavid Howells 		call->state	= RXRPC_CALL_SERVER_SECURING;
3178c3e34a4SDavid Howells 
318248f219cSDavid Howells 	/* Set the channel for this call.  We don't get channel_lock as we're
319248f219cSDavid Howells 	 * only defending against the data_ready handler (which we're called
320248f219cSDavid Howells 	 * from) and the RESPONSE packet parser (which is only really
321248f219cSDavid Howells 	 * interested in call_counter and can cope with a disagreement with the
322248f219cSDavid Howells 	 * call pointer).
3238c3e34a4SDavid Howells 	 */
324248f219cSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
325248f219cSDavid Howells 	conn->channels[chan].call_counter = call->call_id;
326248f219cSDavid Howells 	conn->channels[chan].call_id = call->call_id;
327a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3288c3e34a4SDavid Howells 
32985f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
33085f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
33185f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3328c3e34a4SDavid Howells 
3338c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
3348c3e34a4SDavid Howells 
335248f219cSDavid Howells 	rxrpc_start_call_timer(call);
336248f219cSDavid Howells 	_leave("");
3378c3e34a4SDavid Howells }
3388c3e34a4SDavid Howells 
3398c3e34a4SDavid Howells /*
3408d94aa38SDavid Howells  * Queue a call's work processor, getting a ref to pass to the work queue.
3418d94aa38SDavid Howells  */
3428d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call)
3438d94aa38SDavid Howells {
3448d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3458d94aa38SDavid Howells 	int n = __atomic_add_unless(&call->usage, 1, 0);
3468d94aa38SDavid Howells 	if (n == 0)
3478d94aa38SDavid Howells 		return false;
3488d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3492ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
3508d94aa38SDavid Howells 	else
3518d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3528d94aa38SDavid Howells 	return true;
3538d94aa38SDavid Howells }
3548d94aa38SDavid Howells 
3558d94aa38SDavid Howells /*
3568d94aa38SDavid Howells  * Queue a call's work processor, passing the callers ref to the work queue.
3578d94aa38SDavid Howells  */
3588d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call)
3598d94aa38SDavid Howells {
3608d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3618d94aa38SDavid Howells 	int n = atomic_read(&call->usage);
3628d94aa38SDavid Howells 	ASSERTCMP(n, >=, 1);
3638d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3642ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
3658d94aa38SDavid Howells 	else
3668d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3678d94aa38SDavid Howells 	return true;
3688d94aa38SDavid Howells }
3698d94aa38SDavid Howells 
3708d94aa38SDavid Howells /*
371e34d4234SDavid Howells  * Note the re-emergence of a call.
372e34d4234SDavid Howells  */
373e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call)
374e34d4234SDavid Howells {
375e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
376e34d4234SDavid Howells 	if (call) {
377e34d4234SDavid Howells 		int n = atomic_read(&call->usage);
378e34d4234SDavid Howells 
3792ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
380e34d4234SDavid Howells 	}
381e34d4234SDavid Howells }
382e34d4234SDavid Howells 
383e34d4234SDavid Howells /*
384e34d4234SDavid Howells  * Note the addition of a ref on a call.
385e34d4234SDavid Howells  */
386fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
387e34d4234SDavid Howells {
388e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
389e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
390e34d4234SDavid Howells 
3912ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
392e34d4234SDavid Howells }
393e34d4234SDavid Howells 
394e34d4234SDavid Howells /*
395248f219cSDavid Howells  * Detach a call from its owning socket.
3968c3e34a4SDavid Howells  */
3978d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
3988c3e34a4SDavid Howells {
399248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
400248f219cSDavid Howells 	bool put = false;
401248f219cSDavid Howells 	int i;
402248f219cSDavid Howells 
403248f219cSDavid Howells 	_enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
404248f219cSDavid Howells 
405248f219cSDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
4068c3e34a4SDavid Howells 
407e34d4234SDavid Howells 	rxrpc_see_call(call);
408e34d4234SDavid Howells 
4098c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4108c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4118c3e34a4SDavid Howells 		BUG();
4128c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4138c3e34a4SDavid Howells 
414248f219cSDavid Howells 	del_timer_sync(&call->timer);
4158c3e34a4SDavid Howells 
416248f219cSDavid Howells 	/* Make sure we don't get any more notifications */
417248f219cSDavid Howells 	write_lock_bh(&rx->recvmsg_lock);
418e653cfe4SDavid Howells 
419248f219cSDavid Howells 	if (!list_empty(&call->recvmsg_link)) {
4208c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4218c3e34a4SDavid Howells 		       call, call->events, call->flags);
422248f219cSDavid Howells 		list_del(&call->recvmsg_link);
423248f219cSDavid Howells 		put = true;
424248f219cSDavid Howells 	}
425248f219cSDavid Howells 
426248f219cSDavid Howells 	/* list_empty() must return false in rxrpc_notify_socket() */
427248f219cSDavid Howells 	call->recvmsg_link.next = NULL;
428248f219cSDavid Howells 	call->recvmsg_link.prev = NULL;
429248f219cSDavid Howells 
430248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
431248f219cSDavid Howells 	if (put)
432248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
433248f219cSDavid Howells 
434248f219cSDavid Howells 	write_lock(&rx->call_lock);
435248f219cSDavid Howells 
436248f219cSDavid Howells 	if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4378c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4388c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4398d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_userid);
4408c3e34a4SDavid Howells 	}
4418c3e34a4SDavid Howells 
442248f219cSDavid Howells 	list_del(&call->sock_link);
443248f219cSDavid Howells 	write_unlock(&rx->call_lock);
4448c3e34a4SDavid Howells 
445248f219cSDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4468c3e34a4SDavid Howells 
447248f219cSDavid Howells 	if (conn)
448e653cfe4SDavid Howells 		rxrpc_disconnect_call(call);
449e653cfe4SDavid Howells 
450248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
451248f219cSDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i]);
452248f219cSDavid Howells 		call->rxtx_buffer[i] = NULL;
4538c3e34a4SDavid Howells 	}
4548c3e34a4SDavid Howells 
4558c3e34a4SDavid Howells 	_leave("");
4568c3e34a4SDavid Howells }
4578c3e34a4SDavid Howells 
4588c3e34a4SDavid Howells /*
4598c3e34a4SDavid Howells  * release all the calls associated with a socket
4608c3e34a4SDavid Howells  */
4618c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
4628c3e34a4SDavid Howells {
4638c3e34a4SDavid Howells 	struct rxrpc_call *call;
4648c3e34a4SDavid Howells 
4658c3e34a4SDavid Howells 	_enter("%p", rx);
4668c3e34a4SDavid Howells 
4670360da6dSDavid Howells 	while (!list_empty(&rx->to_be_accepted)) {
4680360da6dSDavid Howells 		call = list_entry(rx->to_be_accepted.next,
4690360da6dSDavid Howells 				  struct rxrpc_call, accept_link);
4700360da6dSDavid Howells 		list_del(&call->accept_link);
4710360da6dSDavid Howells 		rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, ECONNRESET);
4720360da6dSDavid Howells 		rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
4730360da6dSDavid Howells 		rxrpc_release_call(rx, call);
4740360da6dSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
4750360da6dSDavid Howells 	}
4760360da6dSDavid Howells 
477248f219cSDavid Howells 	while (!list_empty(&rx->sock_calls)) {
478248f219cSDavid Howells 		call = list_entry(rx->sock_calls.next,
479248f219cSDavid Howells 				  struct rxrpc_call, sock_link);
480248f219cSDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
481248f219cSDavid Howells 		rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, ECONNRESET);
482248f219cSDavid Howells 		rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
4838d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
484248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
4858c3e34a4SDavid Howells 	}
4868c3e34a4SDavid Howells 
4878c3e34a4SDavid Howells 	_leave("");
4888c3e34a4SDavid Howells }
4898c3e34a4SDavid Howells 
4908c3e34a4SDavid Howells /*
4918c3e34a4SDavid Howells  * release a call
4928c3e34a4SDavid Howells  */
493fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
4948c3e34a4SDavid Howells {
495e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
4962ab27215SDavid Howells 	int n;
497e34d4234SDavid Howells 
4988c3e34a4SDavid Howells 	ASSERT(call != NULL);
4998c3e34a4SDavid Howells 
500e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
5012ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
502e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
503e34d4234SDavid Howells 	if (n == 0) {
5048c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
505248f219cSDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
506e34d4234SDavid Howells 
507248f219cSDavid Howells 		write_lock(&rxrpc_call_lock);
508248f219cSDavid Howells 		list_del_init(&call->link);
509248f219cSDavid Howells 		write_unlock(&rxrpc_call_lock);
510e34d4234SDavid Howells 
5118d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
512e34d4234SDavid Howells 	}
5138c3e34a4SDavid Howells }
5148c3e34a4SDavid Howells 
5158c3e34a4SDavid Howells /*
516dee46364SDavid Howells  * Final call destruction under RCU.
517dee46364SDavid Howells  */
518dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
519dee46364SDavid Howells {
520dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
521dee46364SDavid Howells 
522df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
523248f219cSDavid Howells 	kfree(call->rxtx_buffer);
524248f219cSDavid Howells 	kfree(call->rxtx_annotations);
525dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
526dee46364SDavid Howells }
527dee46364SDavid Howells 
528dee46364SDavid Howells /*
5298c3e34a4SDavid Howells  * clean up a call
5308c3e34a4SDavid Howells  */
53100e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call)
5328c3e34a4SDavid Howells {
533248f219cSDavid Howells 	int i;
5348c3e34a4SDavid Howells 
535248f219cSDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
5368c3e34a4SDavid Howells 
5378c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
5388c3e34a4SDavid Howells 
539248f219cSDavid Howells 	del_timer_sync(&call->timer);
5408c3e34a4SDavid Howells 
5418d94aa38SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
5428c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
543e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
5448c3e34a4SDavid Howells 
545248f219cSDavid Howells 	/* Clean up the Rx/Tx buffer */
546248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
547248f219cSDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i]);
5488c3e34a4SDavid Howells 
5498c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
5508c3e34a4SDavid Howells 
551dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
5528c3e34a4SDavid Howells }
5538c3e34a4SDavid Howells 
5548c3e34a4SDavid Howells /*
5558d94aa38SDavid Howells  * Make sure that all calls are gone.
5568c3e34a4SDavid Howells  */
5578c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
5588c3e34a4SDavid Howells {
5598c3e34a4SDavid Howells 	struct rxrpc_call *call;
5608c3e34a4SDavid Howells 
5618c3e34a4SDavid Howells 	_enter("");
5628d94aa38SDavid Howells 
5638d94aa38SDavid Howells 	if (list_empty(&rxrpc_calls))
5648d94aa38SDavid Howells 		return;
5658d94aa38SDavid Howells 
566248f219cSDavid Howells 	write_lock(&rxrpc_call_lock);
5678c3e34a4SDavid Howells 
5688c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
5698c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
5708c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
5718c3e34a4SDavid Howells 
572e34d4234SDavid Howells 		rxrpc_see_call(call);
5738c3e34a4SDavid Howells 		list_del_init(&call->link);
5748c3e34a4SDavid Howells 
575248f219cSDavid Howells 		pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
5768c3e34a4SDavid Howells 		       call, atomic_read(&call->usage),
5778c3e34a4SDavid Howells 		       rxrpc_call_states[call->state],
5788c3e34a4SDavid Howells 		       call->flags, call->events);
5798c3e34a4SDavid Howells 
580248f219cSDavid Howells 		write_unlock(&rxrpc_call_lock);
5818c3e34a4SDavid Howells 		cond_resched();
582248f219cSDavid Howells 		write_lock(&rxrpc_call_lock);
5838c3e34a4SDavid Howells 	}
5848c3e34a4SDavid Howells 
585248f219cSDavid Howells 	write_unlock(&rxrpc_call_lock);
5868c3e34a4SDavid Howells }
587