xref: /openbmc/linux/net/rxrpc/call_object.c (revision 57494343)
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",
56a84a46d7SDavid Howells 	[rxrpc_call_connected]		= "CON",
57a84a46d7SDavid Howells 	[rxrpc_call_release]		= "RLS",
58fff72429SDavid Howells 	[rxrpc_call_seen]		= "SEE",
59fff72429SDavid Howells 	[rxrpc_call_got]		= "GOT",
60fff72429SDavid Howells 	[rxrpc_call_got_userid]		= "Gus",
61cbd00891SDavid Howells 	[rxrpc_call_got_kernel]		= "Gke",
62fff72429SDavid Howells 	[rxrpc_call_put]		= "PUT",
63fff72429SDavid Howells 	[rxrpc_call_put_userid]		= "Pus",
64cbd00891SDavid Howells 	[rxrpc_call_put_kernel]		= "Pke",
65fff72429SDavid Howells 	[rxrpc_call_put_noqueue]	= "PNQ",
66a84a46d7SDavid Howells 	[rxrpc_call_error]		= "*E*",
67fff72429SDavid Howells };
68fff72429SDavid Howells 
698c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
708c3e34a4SDavid Howells LIST_HEAD(rxrpc_calls);
718c3e34a4SDavid Howells DEFINE_RWLOCK(rxrpc_call_lock);
728c3e34a4SDavid Howells 
73248f219cSDavid Howells static void rxrpc_call_timer_expired(unsigned long _call)
74248f219cSDavid Howells {
75248f219cSDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *)_call;
76248f219cSDavid Howells 
77248f219cSDavid Howells 	_enter("%d", call->debug_id);
78248f219cSDavid Howells 
79fc7ab6d2SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
80fc7ab6d2SDavid Howells 		trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies);
81248f219cSDavid Howells 		rxrpc_queue_call(call);
82248f219cSDavid Howells 	}
83fc7ab6d2SDavid Howells }
848c3e34a4SDavid Howells 
858c3e34a4SDavid Howells /*
868c3e34a4SDavid Howells  * find an extant server call
878c3e34a4SDavid Howells  * - called in process context with IRQs enabled
888c3e34a4SDavid Howells  */
898c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
908c3e34a4SDavid Howells 					      unsigned long user_call_ID)
918c3e34a4SDavid Howells {
928c3e34a4SDavid Howells 	struct rxrpc_call *call;
938c3e34a4SDavid Howells 	struct rb_node *p;
948c3e34a4SDavid Howells 
958c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
968c3e34a4SDavid Howells 
978c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
988c3e34a4SDavid Howells 
998c3e34a4SDavid Howells 	p = rx->calls.rb_node;
1008c3e34a4SDavid Howells 	while (p) {
1018c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
1028c3e34a4SDavid Howells 
1038c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
1048c3e34a4SDavid Howells 			p = p->rb_left;
1058c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
1068c3e34a4SDavid Howells 			p = p->rb_right;
1078c3e34a4SDavid Howells 		else
1088c3e34a4SDavid Howells 			goto found_extant_call;
1098c3e34a4SDavid Howells 	}
1108c3e34a4SDavid Howells 
1118c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1128c3e34a4SDavid Howells 	_leave(" = NULL");
1138c3e34a4SDavid Howells 	return NULL;
1148c3e34a4SDavid Howells 
1158c3e34a4SDavid Howells found_extant_call:
116fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got);
1178c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
1188c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
1198c3e34a4SDavid Howells 	return call;
1208c3e34a4SDavid Howells }
1218c3e34a4SDavid Howells 
1228c3e34a4SDavid Howells /*
1238c3e34a4SDavid Howells  * allocate a new call
1248c3e34a4SDavid Howells  */
12500e90712SDavid Howells struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
1268c3e34a4SDavid Howells {
1278c3e34a4SDavid Howells 	struct rxrpc_call *call;
1288c3e34a4SDavid Howells 
1298c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1308c3e34a4SDavid Howells 	if (!call)
1318c3e34a4SDavid Howells 		return NULL;
1328c3e34a4SDavid Howells 
133248f219cSDavid Howells 	call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
134248f219cSDavid Howells 				    sizeof(struct sk_buff *),
1358c3e34a4SDavid Howells 				    gfp);
136248f219cSDavid Howells 	if (!call->rxtx_buffer)
137248f219cSDavid Howells 		goto nomem;
1388c3e34a4SDavid Howells 
139248f219cSDavid Howells 	call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
140248f219cSDavid Howells 	if (!call->rxtx_annotations)
141248f219cSDavid Howells 		goto nomem_2;
142248f219cSDavid Howells 
143248f219cSDavid Howells 	setup_timer(&call->timer, rxrpc_call_timer_expired,
1448c3e34a4SDavid Howells 		    (unsigned long)call);
1458c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
146999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
14745025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1488c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
149248f219cSDavid Howells 	INIT_LIST_HEAD(&call->recvmsg_link);
150248f219cSDavid Howells 	INIT_LIST_HEAD(&call->sock_link);
15145025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1528c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
1538c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1548c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1558c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
1568c3e34a4SDavid Howells 
1578c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1588c3e34a4SDavid Howells 
159248f219cSDavid Howells 	/* Leave space in the ring to handle a maxed-out jumbo packet */
16075e42126SDavid Howells 	call->rx_winsize = rxrpc_rx_window_size;
161248f219cSDavid Howells 	call->tx_winsize = 16;
162248f219cSDavid Howells 	call->rx_expect_next = 1;
16357494343SDavid Howells 
16457494343SDavid Howells 	if (RXRPC_TX_SMSS > 2190)
16557494343SDavid Howells 		call->cong_cwnd = 2;
16657494343SDavid Howells 	else if (RXRPC_TX_SMSS > 1095)
16757494343SDavid Howells 		call->cong_cwnd = 3;
16857494343SDavid Howells 	else
16957494343SDavid Howells 		call->cong_cwnd = 4;
17057494343SDavid Howells 	call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
1718c3e34a4SDavid Howells 	return call;
172248f219cSDavid Howells 
173248f219cSDavid Howells nomem_2:
174248f219cSDavid Howells 	kfree(call->rxtx_buffer);
175248f219cSDavid Howells nomem:
176248f219cSDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
177248f219cSDavid Howells 	return NULL;
1788c3e34a4SDavid Howells }
1798c3e34a4SDavid Howells 
1808c3e34a4SDavid Howells /*
181999b69f8SDavid Howells  * Allocate a new client call.
1828c3e34a4SDavid Howells  */
183248f219cSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct sockaddr_rxrpc *srx,
1848c3e34a4SDavid Howells 						  gfp_t gfp)
1858c3e34a4SDavid Howells {
1868c3e34a4SDavid Howells 	struct rxrpc_call *call;
18757494343SDavid Howells 	ktime_t now;
1888c3e34a4SDavid Howells 
1898c3e34a4SDavid Howells 	_enter("");
1908c3e34a4SDavid Howells 
1918c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1928c3e34a4SDavid Howells 	if (!call)
1938c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
194999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
195999b69f8SDavid Howells 	call->service_id = srx->srx_service;
19671f3ca40SDavid Howells 	call->tx_phase = true;
19757494343SDavid Howells 	now = ktime_get_real();
19857494343SDavid Howells 	call->acks_latest_ts = now;
19957494343SDavid Howells 	call->cong_tstamp = now;
200999b69f8SDavid Howells 
201999b69f8SDavid Howells 	_leave(" = %p", call);
202999b69f8SDavid Howells 	return call;
203999b69f8SDavid Howells }
204999b69f8SDavid Howells 
205999b69f8SDavid Howells /*
206248f219cSDavid Howells  * Initiate the call ack/resend/expiry timer.
207999b69f8SDavid Howells  */
208248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call)
209999b69f8SDavid Howells {
210248f219cSDavid Howells 	unsigned long expire_at;
211999b69f8SDavid Howells 
212248f219cSDavid Howells 	expire_at = jiffies + rxrpc_max_call_lifetime;
213248f219cSDavid Howells 	call->expire_at = expire_at;
214248f219cSDavid Howells 	call->ack_at = expire_at;
215248f219cSDavid Howells 	call->resend_at = expire_at;
21601a88f7fSDavid Howells 	call->timer.expires = expire_at + 1;
217fc7ab6d2SDavid Howells 	rxrpc_set_timer(call, rxrpc_timer_begin);
2188c3e34a4SDavid Howells }
2198c3e34a4SDavid Howells 
2208c3e34a4SDavid Howells /*
2218c3e34a4SDavid Howells  * set up a call for the given data
2228c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2238c3e34a4SDavid Howells  */
2248c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
22519ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
226999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2278c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2288c3e34a4SDavid Howells 					 gfp_t gfp)
2298c3e34a4SDavid Howells {
2308c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2318c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
232e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
233999b69f8SDavid Howells 	int ret;
2348c3e34a4SDavid Howells 
235999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2368c3e34a4SDavid Howells 
237248f219cSDavid Howells 	call = rxrpc_alloc_client_call(srx, gfp);
2388c3e34a4SDavid Howells 	if (IS_ERR(call)) {
2398c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2408c3e34a4SDavid Howells 		return call;
2418c3e34a4SDavid Howells 	}
2428c3e34a4SDavid Howells 
243a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage),
244a84a46d7SDavid Howells 			 here, (const void *)user_call_ID);
245e34d4234SDavid Howells 
246999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2478c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2488c3e34a4SDavid Howells 
2498c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2508c3e34a4SDavid Howells 	parent = NULL;
2518c3e34a4SDavid Howells 	while (*pp) {
2528c3e34a4SDavid Howells 		parent = *pp;
2538c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2548c3e34a4SDavid Howells 
2558c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2568c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2578c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2588c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2598c3e34a4SDavid Howells 		else
260357f5ef6SDavid Howells 			goto error_dup_user_ID;
2618c3e34a4SDavid Howells 	}
2628c3e34a4SDavid Howells 
263248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
264357f5ef6SDavid Howells 	call->user_call_ID = user_call_ID;
265357f5ef6SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
266fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got_userid);
2678c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2688c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
269248f219cSDavid Howells 	list_add(&call->sock_link, &rx->sock_calls);
270248f219cSDavid Howells 
2718c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2728c3e34a4SDavid Howells 
273248f219cSDavid Howells 	write_lock(&rxrpc_call_lock);
2748c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
275248f219cSDavid Howells 	write_unlock(&rxrpc_call_lock);
2768c3e34a4SDavid Howells 
277248f219cSDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
278248f219cSDavid Howells 	 * including channel number and call ID.
279248f219cSDavid Howells 	 */
280248f219cSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
281999b69f8SDavid Howells 	if (ret < 0)
282999b69f8SDavid Howells 		goto error;
283999b69f8SDavid Howells 
284a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
285a84a46d7SDavid Howells 			 here, ERR_PTR(ret));
286a84a46d7SDavid Howells 
287248f219cSDavid Howells 	spin_lock_bh(&call->conn->params.peer->lock);
288248f219cSDavid Howells 	hlist_add_head(&call->error_link,
289248f219cSDavid Howells 		       &call->conn->params.peer->error_targets);
290248f219cSDavid Howells 	spin_unlock_bh(&call->conn->params.peer->lock);
291248f219cSDavid Howells 
292248f219cSDavid Howells 	rxrpc_start_call_timer(call);
293248f219cSDavid Howells 
2948c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2958c3e34a4SDavid Howells 
2968c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2978c3e34a4SDavid Howells 	return call;
2988c3e34a4SDavid Howells 
2998c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
3008c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
3018c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
3028c3e34a4SDavid Howells 	 * same time in different threads.
3038c3e34a4SDavid Howells 	 */
304357f5ef6SDavid Howells error_dup_user_ID:
3058c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
3068d94aa38SDavid Howells 	ret = -EEXIST;
307357f5ef6SDavid Howells 
308357f5ef6SDavid Howells error:
309357f5ef6SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
310357f5ef6SDavid Howells 				    RX_CALL_DEAD, ret);
311a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
312a84a46d7SDavid Howells 			 here, ERR_PTR(ret));
313357f5ef6SDavid Howells 	rxrpc_release_call(rx, call);
314357f5ef6SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
315357f5ef6SDavid Howells 	_leave(" = %d", ret);
316357f5ef6SDavid Howells 	return ERR_PTR(ret);
3178c3e34a4SDavid Howells }
3188c3e34a4SDavid Howells 
3198c3e34a4SDavid Howells /*
320248f219cSDavid Howells  * Set up an incoming call.  call->conn points to the connection.
321248f219cSDavid Howells  * This is called in BH context and isn't allowed to fail.
3228c3e34a4SDavid Howells  */
323248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx,
324248f219cSDavid Howells 			 struct rxrpc_call *call,
32542886ffeSDavid Howells 			 struct sk_buff *skb)
3268c3e34a4SDavid Howells {
327248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
32842886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
329248f219cSDavid Howells 	u32 chan;
3308c3e34a4SDavid Howells 
331248f219cSDavid Howells 	_enter(",%d", call->conn->debug_id);
3328c3e34a4SDavid Howells 
333248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
334248f219cSDavid Howells 	call->call_id		= sp->hdr.callNumber;
335248f219cSDavid Howells 	call->service_id	= sp->hdr.serviceId;
336248f219cSDavid Howells 	call->cid		= sp->hdr.cid;
337248f219cSDavid Howells 	call->state		= RXRPC_CALL_SERVER_ACCEPTING;
338248f219cSDavid Howells 	if (sp->hdr.securityIndex > 0)
339248f219cSDavid Howells 		call->state	= RXRPC_CALL_SERVER_SECURING;
34057494343SDavid Howells 	call->cong_tstamp	= skb->tstamp;
3418c3e34a4SDavid Howells 
342248f219cSDavid Howells 	/* Set the channel for this call.  We don't get channel_lock as we're
343248f219cSDavid Howells 	 * only defending against the data_ready handler (which we're called
344248f219cSDavid Howells 	 * from) and the RESPONSE packet parser (which is only really
345248f219cSDavid Howells 	 * interested in call_counter and can cope with a disagreement with the
346248f219cSDavid Howells 	 * call pointer).
3478c3e34a4SDavid Howells 	 */
348248f219cSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
349248f219cSDavid Howells 	conn->channels[chan].call_counter = call->call_id;
350248f219cSDavid Howells 	conn->channels[chan].call_id = call->call_id;
351a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3528c3e34a4SDavid Howells 
35385f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
35485f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
35585f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3568c3e34a4SDavid Howells 
3578c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
3588c3e34a4SDavid Howells 
359248f219cSDavid Howells 	rxrpc_start_call_timer(call);
360248f219cSDavid Howells 	_leave("");
3618c3e34a4SDavid Howells }
3628c3e34a4SDavid Howells 
3638c3e34a4SDavid Howells /*
3648d94aa38SDavid Howells  * Queue a call's work processor, getting a ref to pass to the work queue.
3658d94aa38SDavid Howells  */
3668d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call)
3678d94aa38SDavid Howells {
3688d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3698d94aa38SDavid Howells 	int n = __atomic_add_unless(&call->usage, 1, 0);
3708d94aa38SDavid Howells 	if (n == 0)
3718d94aa38SDavid Howells 		return false;
3728d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3732ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
3748d94aa38SDavid Howells 	else
3758d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3768d94aa38SDavid Howells 	return true;
3778d94aa38SDavid Howells }
3788d94aa38SDavid Howells 
3798d94aa38SDavid Howells /*
3808d94aa38SDavid Howells  * Queue a call's work processor, passing the callers ref to the work queue.
3818d94aa38SDavid Howells  */
3828d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call)
3838d94aa38SDavid Howells {
3848d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3858d94aa38SDavid Howells 	int n = atomic_read(&call->usage);
3868d94aa38SDavid Howells 	ASSERTCMP(n, >=, 1);
3878d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3882ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
3898d94aa38SDavid Howells 	else
3908d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3918d94aa38SDavid Howells 	return true;
3928d94aa38SDavid Howells }
3938d94aa38SDavid Howells 
3948d94aa38SDavid Howells /*
395e34d4234SDavid Howells  * Note the re-emergence of a call.
396e34d4234SDavid Howells  */
397e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call)
398e34d4234SDavid Howells {
399e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
400e34d4234SDavid Howells 	if (call) {
401e34d4234SDavid Howells 		int n = atomic_read(&call->usage);
402e34d4234SDavid Howells 
4032ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
404e34d4234SDavid Howells 	}
405e34d4234SDavid Howells }
406e34d4234SDavid Howells 
407e34d4234SDavid Howells /*
408e34d4234SDavid Howells  * Note the addition of a ref on a call.
409e34d4234SDavid Howells  */
410fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
411e34d4234SDavid Howells {
412e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
413e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
414e34d4234SDavid Howells 
4152ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
416e34d4234SDavid Howells }
417e34d4234SDavid Howells 
418e34d4234SDavid Howells /*
419248f219cSDavid Howells  * Detach a call from its owning socket.
4208c3e34a4SDavid Howells  */
4218d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
4228c3e34a4SDavid Howells {
423a84a46d7SDavid Howells 	const void *here = __builtin_return_address(0);
424248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
425248f219cSDavid Howells 	bool put = false;
426248f219cSDavid Howells 	int i;
427248f219cSDavid Howells 
428248f219cSDavid Howells 	_enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
429248f219cSDavid Howells 
430a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
431a84a46d7SDavid Howells 			 here, (const void *)call->flags);
4328c3e34a4SDavid Howells 
433a84a46d7SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
434e34d4234SDavid Howells 
4358c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4368c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4378c3e34a4SDavid Howells 		BUG();
4388c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4398c3e34a4SDavid Howells 
440248f219cSDavid Howells 	del_timer_sync(&call->timer);
4418c3e34a4SDavid Howells 
442248f219cSDavid Howells 	/* Make sure we don't get any more notifications */
443248f219cSDavid Howells 	write_lock_bh(&rx->recvmsg_lock);
444e653cfe4SDavid Howells 
445248f219cSDavid Howells 	if (!list_empty(&call->recvmsg_link)) {
4468c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4478c3e34a4SDavid Howells 		       call, call->events, call->flags);
448248f219cSDavid Howells 		list_del(&call->recvmsg_link);
449248f219cSDavid Howells 		put = true;
450248f219cSDavid Howells 	}
451248f219cSDavid Howells 
452248f219cSDavid Howells 	/* list_empty() must return false in rxrpc_notify_socket() */
453248f219cSDavid Howells 	call->recvmsg_link.next = NULL;
454248f219cSDavid Howells 	call->recvmsg_link.prev = NULL;
455248f219cSDavid Howells 
456248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
457248f219cSDavid Howells 	if (put)
458248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
459248f219cSDavid Howells 
460248f219cSDavid Howells 	write_lock(&rx->call_lock);
461248f219cSDavid Howells 
462248f219cSDavid Howells 	if (test_and_clear_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));
4658d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_userid);
4668c3e34a4SDavid Howells 	}
4678c3e34a4SDavid Howells 
468248f219cSDavid Howells 	list_del(&call->sock_link);
469248f219cSDavid Howells 	write_unlock(&rx->call_lock);
4708c3e34a4SDavid Howells 
471248f219cSDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4728c3e34a4SDavid Howells 
473248f219cSDavid Howells 	if (conn)
474e653cfe4SDavid Howells 		rxrpc_disconnect_call(call);
475e653cfe4SDavid Howells 
476248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
47771f3ca40SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i],
47871f3ca40SDavid Howells 			       (call->tx_phase ? rxrpc_skb_tx_cleaned :
47971f3ca40SDavid Howells 				rxrpc_skb_rx_cleaned));
480248f219cSDavid Howells 		call->rxtx_buffer[i] = NULL;
4818c3e34a4SDavid Howells 	}
4828c3e34a4SDavid Howells 
4838c3e34a4SDavid Howells 	_leave("");
4848c3e34a4SDavid Howells }
4858c3e34a4SDavid Howells 
4868c3e34a4SDavid Howells /*
4878c3e34a4SDavid Howells  * release all the calls associated with a socket
4888c3e34a4SDavid Howells  */
4898c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
4908c3e34a4SDavid Howells {
4918c3e34a4SDavid Howells 	struct rxrpc_call *call;
4928c3e34a4SDavid Howells 
4938c3e34a4SDavid Howells 	_enter("%p", rx);
4948c3e34a4SDavid Howells 
4950360da6dSDavid Howells 	while (!list_empty(&rx->to_be_accepted)) {
4960360da6dSDavid Howells 		call = list_entry(rx->to_be_accepted.next,
4970360da6dSDavid Howells 				  struct rxrpc_call, accept_link);
4980360da6dSDavid Howells 		list_del(&call->accept_link);
4990360da6dSDavid Howells 		rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, ECONNRESET);
5000360da6dSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5010360da6dSDavid Howells 	}
5020360da6dSDavid Howells 
503248f219cSDavid Howells 	while (!list_empty(&rx->sock_calls)) {
504248f219cSDavid Howells 		call = list_entry(rx->sock_calls.next,
505248f219cSDavid Howells 				  struct rxrpc_call, sock_link);
506248f219cSDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
507248f219cSDavid Howells 		rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, ECONNRESET);
508248f219cSDavid Howells 		rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
5098d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
510248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5118c3e34a4SDavid Howells 	}
5128c3e34a4SDavid Howells 
5138c3e34a4SDavid Howells 	_leave("");
5148c3e34a4SDavid Howells }
5158c3e34a4SDavid Howells 
5168c3e34a4SDavid Howells /*
5178c3e34a4SDavid Howells  * release a call
5188c3e34a4SDavid Howells  */
519fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
5208c3e34a4SDavid Howells {
521e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
5222ab27215SDavid Howells 	int n;
523e34d4234SDavid Howells 
5248c3e34a4SDavid Howells 	ASSERT(call != NULL);
5258c3e34a4SDavid Howells 
526e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
5272ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
528e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
529e34d4234SDavid Howells 	if (n == 0) {
5308c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
531248f219cSDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
532e34d4234SDavid Howells 
533248f219cSDavid Howells 		write_lock(&rxrpc_call_lock);
534248f219cSDavid Howells 		list_del_init(&call->link);
535248f219cSDavid Howells 		write_unlock(&rxrpc_call_lock);
536e34d4234SDavid Howells 
5378d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
538e34d4234SDavid Howells 	}
5398c3e34a4SDavid Howells }
5408c3e34a4SDavid Howells 
5418c3e34a4SDavid Howells /*
542dee46364SDavid Howells  * Final call destruction under RCU.
543dee46364SDavid Howells  */
544dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
545dee46364SDavid Howells {
546dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
547dee46364SDavid Howells 
548df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
549248f219cSDavid Howells 	kfree(call->rxtx_buffer);
550248f219cSDavid Howells 	kfree(call->rxtx_annotations);
551dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
552dee46364SDavid Howells }
553dee46364SDavid Howells 
554dee46364SDavid Howells /*
5558c3e34a4SDavid Howells  * clean up a call
5568c3e34a4SDavid Howells  */
55700e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call)
5588c3e34a4SDavid Howells {
559248f219cSDavid Howells 	int i;
5608c3e34a4SDavid Howells 
561248f219cSDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
5628c3e34a4SDavid Howells 
5638c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
5648c3e34a4SDavid Howells 
565248f219cSDavid Howells 	del_timer_sync(&call->timer);
5668c3e34a4SDavid Howells 
5678d94aa38SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
5688c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
569e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
5708c3e34a4SDavid Howells 
571248f219cSDavid Howells 	/* Clean up the Rx/Tx buffer */
572248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
57371f3ca40SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i],
57471f3ca40SDavid Howells 			       (call->tx_phase ? rxrpc_skb_tx_cleaned :
57571f3ca40SDavid Howells 				rxrpc_skb_rx_cleaned));
5768c3e34a4SDavid Howells 
57771f3ca40SDavid Howells 	rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned);
5788c3e34a4SDavid Howells 
579dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
5808c3e34a4SDavid Howells }
5818c3e34a4SDavid Howells 
5828c3e34a4SDavid Howells /*
5838d94aa38SDavid Howells  * Make sure that all calls are gone.
5848c3e34a4SDavid Howells  */
5858c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
5868c3e34a4SDavid Howells {
5878c3e34a4SDavid Howells 	struct rxrpc_call *call;
5888c3e34a4SDavid Howells 
5898c3e34a4SDavid Howells 	_enter("");
5908d94aa38SDavid Howells 
5918d94aa38SDavid Howells 	if (list_empty(&rxrpc_calls))
5928d94aa38SDavid Howells 		return;
5938d94aa38SDavid Howells 
594248f219cSDavid Howells 	write_lock(&rxrpc_call_lock);
5958c3e34a4SDavid Howells 
5968c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
5978c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
5988c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
5998c3e34a4SDavid Howells 
600e34d4234SDavid Howells 		rxrpc_see_call(call);
6018c3e34a4SDavid Howells 		list_del_init(&call->link);
6028c3e34a4SDavid Howells 
603248f219cSDavid Howells 		pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
6048c3e34a4SDavid Howells 		       call, atomic_read(&call->usage),
6058c3e34a4SDavid Howells 		       rxrpc_call_states[call->state],
6068c3e34a4SDavid Howells 		       call->flags, call->events);
6078c3e34a4SDavid Howells 
608248f219cSDavid Howells 		write_unlock(&rxrpc_call_lock);
6098c3e34a4SDavid Howells 		cond_resched();
610248f219cSDavid Howells 		write_lock(&rxrpc_call_lock);
6118c3e34a4SDavid Howells 	}
6128c3e34a4SDavid Howells 
613248f219cSDavid Howells 	write_unlock(&rxrpc_call_lock);
6148c3e34a4SDavid Howells }
615