xref: /openbmc/linux/net/rxrpc/call_object.c (revision e99e88a9)
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 const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
23999b69f8SDavid Howells 	[RXRPC_CALL_UNINITIALISED]		= "Uninit  ",
24999b69f8SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_CONN]		= "ClWtConn",
258c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_SEND_REQUEST]	= "ClSndReq",
268c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_REPLY]		= "ClAwtRpl",
278c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_RECV_REPLY]		= "ClRcvRpl",
2800e90712SDavid Howells 	[RXRPC_CALL_SERVER_PREALLOC]		= "SvPrealc",
298c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SECURING]		= "SvSecure",
308c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACCEPTING]		= "SvAccept",
318c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_RECV_REQUEST]	= "SvRcvReq",
328c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACK_REQUEST]		= "SvAckReq",
338c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SEND_REPLY]		= "SvSndRpl",
348c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_AWAIT_ACK]		= "SvAwtACK",
358c3e34a4SDavid Howells 	[RXRPC_CALL_COMPLETE]			= "Complete",
36f5c17aaeSDavid Howells };
37f5c17aaeSDavid Howells 
38f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
39f5c17aaeSDavid Howells 	[RXRPC_CALL_SUCCEEDED]			= "Complete",
408c3e34a4SDavid Howells 	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
418c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
42f5c17aaeSDavid Howells 	[RXRPC_CALL_LOCAL_ERROR]		= "LocError",
438c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
448c3e34a4SDavid Howells };
458c3e34a4SDavid Howells 
468c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
478c3e34a4SDavid Howells 
48e99e88a9SKees Cook static void rxrpc_call_timer_expired(struct timer_list *t)
49248f219cSDavid Howells {
50e99e88a9SKees Cook 	struct rxrpc_call *call = from_timer(call, t, timer);
51248f219cSDavid Howells 
52248f219cSDavid Howells 	_enter("%d", call->debug_id);
53248f219cSDavid Howells 
54405dea1dSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE)
55405dea1dSDavid Howells 		rxrpc_set_timer(call, rxrpc_timer_expired, ktime_get_real());
56fc7ab6d2SDavid Howells }
578c3e34a4SDavid Howells 
588c3e34a4SDavid Howells /*
598c3e34a4SDavid Howells  * find an extant server call
608c3e34a4SDavid Howells  * - called in process context with IRQs enabled
618c3e34a4SDavid Howells  */
628c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
638c3e34a4SDavid Howells 					      unsigned long user_call_ID)
648c3e34a4SDavid Howells {
658c3e34a4SDavid Howells 	struct rxrpc_call *call;
668c3e34a4SDavid Howells 	struct rb_node *p;
678c3e34a4SDavid Howells 
688c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
698c3e34a4SDavid Howells 
708c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
718c3e34a4SDavid Howells 
728c3e34a4SDavid Howells 	p = rx->calls.rb_node;
738c3e34a4SDavid Howells 	while (p) {
748c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
758c3e34a4SDavid Howells 
768c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
778c3e34a4SDavid Howells 			p = p->rb_left;
788c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
798c3e34a4SDavid Howells 			p = p->rb_right;
808c3e34a4SDavid Howells 		else
818c3e34a4SDavid Howells 			goto found_extant_call;
828c3e34a4SDavid Howells 	}
838c3e34a4SDavid Howells 
848c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
858c3e34a4SDavid Howells 	_leave(" = NULL");
868c3e34a4SDavid Howells 	return NULL;
878c3e34a4SDavid Howells 
888c3e34a4SDavid Howells found_extant_call:
89fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got);
908c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
918c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
928c3e34a4SDavid Howells 	return call;
938c3e34a4SDavid Howells }
948c3e34a4SDavid Howells 
958c3e34a4SDavid Howells /*
968c3e34a4SDavid Howells  * allocate a new call
978c3e34a4SDavid Howells  */
9800e90712SDavid Howells struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
998c3e34a4SDavid Howells {
1008c3e34a4SDavid Howells 	struct rxrpc_call *call;
1018c3e34a4SDavid Howells 
1028c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1038c3e34a4SDavid Howells 	if (!call)
1048c3e34a4SDavid Howells 		return NULL;
1058c3e34a4SDavid Howells 
106248f219cSDavid Howells 	call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
107248f219cSDavid Howells 				    sizeof(struct sk_buff *),
1088c3e34a4SDavid Howells 				    gfp);
109248f219cSDavid Howells 	if (!call->rxtx_buffer)
110248f219cSDavid Howells 		goto nomem;
1118c3e34a4SDavid Howells 
112248f219cSDavid Howells 	call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
113248f219cSDavid Howells 	if (!call->rxtx_annotations)
114248f219cSDavid Howells 		goto nomem_2;
115248f219cSDavid Howells 
116540b1c48SDavid Howells 	mutex_init(&call->user_mutex);
117e99e88a9SKees Cook 	timer_setup(&call->timer, rxrpc_call_timer_expired, 0);
1188c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
119999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
12045025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1218c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
122248f219cSDavid Howells 	INIT_LIST_HEAD(&call->recvmsg_link);
123248f219cSDavid Howells 	INIT_LIST_HEAD(&call->sock_link);
12445025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1258c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
12620acbd9aSDavid Howells 	spin_lock_init(&call->notify_lock);
1278c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1288c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
1298c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
130e754eba6SDavid Howells 	call->tx_total_len = -1;
1318c3e34a4SDavid Howells 
1328c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1338c3e34a4SDavid Howells 
134248f219cSDavid Howells 	/* Leave space in the ring to handle a maxed-out jumbo packet */
13575e42126SDavid Howells 	call->rx_winsize = rxrpc_rx_window_size;
136248f219cSDavid Howells 	call->tx_winsize = 16;
137248f219cSDavid Howells 	call->rx_expect_next = 1;
13857494343SDavid Howells 
13957494343SDavid Howells 	call->cong_cwnd = 2;
14057494343SDavid Howells 	call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
1418c3e34a4SDavid Howells 	return call;
142248f219cSDavid Howells 
143248f219cSDavid Howells nomem_2:
144248f219cSDavid Howells 	kfree(call->rxtx_buffer);
145248f219cSDavid Howells nomem:
146248f219cSDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
147248f219cSDavid Howells 	return NULL;
1488c3e34a4SDavid Howells }
1498c3e34a4SDavid Howells 
1508c3e34a4SDavid Howells /*
151999b69f8SDavid Howells  * Allocate a new client call.
1528c3e34a4SDavid Howells  */
153248f219cSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct sockaddr_rxrpc *srx,
1548c3e34a4SDavid Howells 						  gfp_t gfp)
1558c3e34a4SDavid Howells {
1568c3e34a4SDavid Howells 	struct rxrpc_call *call;
15757494343SDavid Howells 	ktime_t now;
1588c3e34a4SDavid Howells 
1598c3e34a4SDavid Howells 	_enter("");
1608c3e34a4SDavid Howells 
1618c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1628c3e34a4SDavid Howells 	if (!call)
1638c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
164999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
165999b69f8SDavid Howells 	call->service_id = srx->srx_service;
16671f3ca40SDavid Howells 	call->tx_phase = true;
16757494343SDavid Howells 	now = ktime_get_real();
16857494343SDavid Howells 	call->acks_latest_ts = now;
16957494343SDavid Howells 	call->cong_tstamp = now;
170999b69f8SDavid Howells 
171999b69f8SDavid Howells 	_leave(" = %p", call);
172999b69f8SDavid Howells 	return call;
173999b69f8SDavid Howells }
174999b69f8SDavid Howells 
175999b69f8SDavid Howells /*
176248f219cSDavid Howells  * Initiate the call ack/resend/expiry timer.
177999b69f8SDavid Howells  */
178248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call)
179999b69f8SDavid Howells {
180df0adc78SDavid Howells 	ktime_t now = ktime_get_real(), expire_at;
181999b69f8SDavid Howells 
182df0adc78SDavid Howells 	expire_at = ktime_add_ms(now, rxrpc_max_call_lifetime);
183248f219cSDavid Howells 	call->expire_at = expire_at;
184248f219cSDavid Howells 	call->ack_at = expire_at;
185a5af7e1fSDavid Howells 	call->ping_at = expire_at;
186248f219cSDavid Howells 	call->resend_at = expire_at;
187df0adc78SDavid Howells 	call->timer.expires = jiffies + LONG_MAX / 2;
188df0adc78SDavid Howells 	rxrpc_set_timer(call, rxrpc_timer_begin, now);
1898c3e34a4SDavid Howells }
1908c3e34a4SDavid Howells 
1918c3e34a4SDavid Howells /*
192540b1c48SDavid Howells  * Set up a call for the given parameters.
193540b1c48SDavid Howells  * - Called with the socket lock held, which it must release.
194540b1c48SDavid Howells  * - If it returns a call, the call's lock will need releasing by the caller.
1958c3e34a4SDavid Howells  */
1968c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
19719ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
198999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
1998c3e34a4SDavid Howells 					 unsigned long user_call_ID,
200e754eba6SDavid Howells 					 s64 tx_total_len,
2018c3e34a4SDavid Howells 					 gfp_t gfp)
202540b1c48SDavid Howells 	__releases(&rx->sk.sk_lock.slock)
2038c3e34a4SDavid Howells {
2048c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2052baec2c3SDavid Howells 	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
2068c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
207e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
208999b69f8SDavid Howells 	int ret;
2098c3e34a4SDavid Howells 
210999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2118c3e34a4SDavid Howells 
212248f219cSDavid Howells 	call = rxrpc_alloc_client_call(srx, gfp);
2138c3e34a4SDavid Howells 	if (IS_ERR(call)) {
214540b1c48SDavid Howells 		release_sock(&rx->sk);
2158c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2168c3e34a4SDavid Howells 		return call;
2178c3e34a4SDavid Howells 	}
2188c3e34a4SDavid Howells 
219e754eba6SDavid Howells 	call->tx_total_len = tx_total_len;
220a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage),
221a84a46d7SDavid Howells 			 here, (const void *)user_call_ID);
222e34d4234SDavid Howells 
223540b1c48SDavid Howells 	/* We need to protect a partially set up call against the user as we
224540b1c48SDavid Howells 	 * will be acting outside the socket lock.
225540b1c48SDavid Howells 	 */
226540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
227540b1c48SDavid 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 
2552baec2c3SDavid Howells 	write_lock(&rxnet->call_lock);
2562baec2c3SDavid Howells 	list_add_tail(&call->link, &rxnet->calls);
2572baec2c3SDavid Howells 	write_unlock(&rxnet->call_lock);
2588c3e34a4SDavid Howells 
259540b1c48SDavid Howells 	/* From this point on, the call is protected by its own lock. */
260540b1c48SDavid Howells 	release_sock(&rx->sk);
261540b1c48SDavid Howells 
262248f219cSDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
263248f219cSDavid Howells 	 * including channel number and call ID.
264248f219cSDavid Howells 	 */
265248f219cSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
266999b69f8SDavid Howells 	if (ret < 0)
267999b69f8SDavid Howells 		goto error;
268999b69f8SDavid Howells 
269a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
27054fde423SDavid Howells 			 here, NULL);
271a84a46d7SDavid Howells 
272248f219cSDavid Howells 	rxrpc_start_call_timer(call);
273248f219cSDavid Howells 
2748c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2758c3e34a4SDavid Howells 
2768c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2778c3e34a4SDavid Howells 	return call;
2788c3e34a4SDavid Howells 
2798c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2808c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2818c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2828c3e34a4SDavid Howells 	 * same time in different threads.
2838c3e34a4SDavid Howells 	 */
284357f5ef6SDavid Howells error_dup_user_ID:
2858c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
286540b1c48SDavid Howells 	release_sock(&rx->sk);
2878d94aa38SDavid Howells 	ret = -EEXIST;
288357f5ef6SDavid Howells 
289357f5ef6SDavid Howells error:
290357f5ef6SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
291357f5ef6SDavid Howells 				    RX_CALL_DEAD, ret);
292a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
293a84a46d7SDavid Howells 			 here, ERR_PTR(ret));
294357f5ef6SDavid Howells 	rxrpc_release_call(rx, call);
295540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
296357f5ef6SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
297357f5ef6SDavid Howells 	_leave(" = %d", ret);
298357f5ef6SDavid Howells 	return ERR_PTR(ret);
2998c3e34a4SDavid Howells }
3008c3e34a4SDavid Howells 
3018c3e34a4SDavid Howells /*
302c038a58cSDavid Howells  * Retry a call to a new address.  It is expected that the Tx queue of the call
303c038a58cSDavid Howells  * will contain data previously packaged for an old call.
304c038a58cSDavid Howells  */
305c038a58cSDavid Howells int rxrpc_retry_client_call(struct rxrpc_sock *rx,
306c038a58cSDavid Howells 			    struct rxrpc_call *call,
307c038a58cSDavid Howells 			    struct rxrpc_conn_parameters *cp,
308c038a58cSDavid Howells 			    struct sockaddr_rxrpc *srx,
309c038a58cSDavid Howells 			    gfp_t gfp)
310c038a58cSDavid Howells {
311c038a58cSDavid Howells 	const void *here = __builtin_return_address(0);
312c038a58cSDavid Howells 	int ret;
313c038a58cSDavid Howells 
314c038a58cSDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
315c038a58cSDavid Howells 	 * including channel number and call ID.
316c038a58cSDavid Howells 	 */
317c038a58cSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
318c038a58cSDavid Howells 	if (ret < 0)
319c038a58cSDavid Howells 		goto error;
320c038a58cSDavid Howells 
321c038a58cSDavid Howells 	trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
322c038a58cSDavid Howells 			 here, NULL);
323c038a58cSDavid Howells 
324c038a58cSDavid Howells 	rxrpc_start_call_timer(call);
325c038a58cSDavid Howells 
326c038a58cSDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
327c038a58cSDavid Howells 
328c038a58cSDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
329c038a58cSDavid Howells 		rxrpc_queue_call(call);
330c038a58cSDavid Howells 
331c038a58cSDavid Howells 	_leave(" = 0");
332c038a58cSDavid Howells 	return 0;
333c038a58cSDavid Howells 
334c038a58cSDavid Howells error:
335c038a58cSDavid Howells 	rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
336c038a58cSDavid Howells 				  RX_CALL_DEAD, ret);
337c038a58cSDavid Howells 	trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
338c038a58cSDavid Howells 			 here, ERR_PTR(ret));
339c038a58cSDavid Howells 	_leave(" = %d", ret);
340c038a58cSDavid Howells 	return ret;
341c038a58cSDavid Howells }
342c038a58cSDavid Howells 
343c038a58cSDavid Howells /*
344248f219cSDavid Howells  * Set up an incoming call.  call->conn points to the connection.
345248f219cSDavid Howells  * This is called in BH context and isn't allowed to fail.
3468c3e34a4SDavid Howells  */
347248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx,
348248f219cSDavid Howells 			 struct rxrpc_call *call,
34942886ffeSDavid Howells 			 struct sk_buff *skb)
3508c3e34a4SDavid Howells {
351248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
35242886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
353248f219cSDavid Howells 	u32 chan;
3548c3e34a4SDavid Howells 
355248f219cSDavid Howells 	_enter(",%d", call->conn->debug_id);
3568c3e34a4SDavid Howells 
357248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
358248f219cSDavid Howells 	call->call_id		= sp->hdr.callNumber;
359248f219cSDavid Howells 	call->service_id	= sp->hdr.serviceId;
360248f219cSDavid Howells 	call->cid		= sp->hdr.cid;
361248f219cSDavid Howells 	call->state		= RXRPC_CALL_SERVER_ACCEPTING;
362248f219cSDavid Howells 	if (sp->hdr.securityIndex > 0)
363248f219cSDavid Howells 		call->state	= RXRPC_CALL_SERVER_SECURING;
36457494343SDavid Howells 	call->cong_tstamp	= skb->tstamp;
3658c3e34a4SDavid Howells 
366248f219cSDavid Howells 	/* Set the channel for this call.  We don't get channel_lock as we're
367248f219cSDavid Howells 	 * only defending against the data_ready handler (which we're called
368248f219cSDavid Howells 	 * from) and the RESPONSE packet parser (which is only really
369248f219cSDavid Howells 	 * interested in call_counter and can cope with a disagreement with the
370248f219cSDavid Howells 	 * call pointer).
3718c3e34a4SDavid Howells 	 */
372248f219cSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
373248f219cSDavid Howells 	conn->channels[chan].call_counter = call->call_id;
374248f219cSDavid Howells 	conn->channels[chan].call_id = call->call_id;
375a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3768c3e34a4SDavid Howells 
37785f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
37885f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
37985f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3808c3e34a4SDavid Howells 
3818c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
3828c3e34a4SDavid Howells 
383248f219cSDavid Howells 	rxrpc_start_call_timer(call);
384248f219cSDavid Howells 	_leave("");
3858c3e34a4SDavid Howells }
3868c3e34a4SDavid Howells 
3878c3e34a4SDavid Howells /*
3888d94aa38SDavid Howells  * Queue a call's work processor, getting a ref to pass to the work queue.
3898d94aa38SDavid Howells  */
3908d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call)
3918d94aa38SDavid Howells {
3928d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3938d94aa38SDavid Howells 	int n = __atomic_add_unless(&call->usage, 1, 0);
3948d94aa38SDavid Howells 	if (n == 0)
3958d94aa38SDavid Howells 		return false;
3968d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3972ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
3988d94aa38SDavid Howells 	else
3998d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4008d94aa38SDavid Howells 	return true;
4018d94aa38SDavid Howells }
4028d94aa38SDavid Howells 
4038d94aa38SDavid Howells /*
4048d94aa38SDavid Howells  * Queue a call's work processor, passing the callers ref to the work queue.
4058d94aa38SDavid Howells  */
4068d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call)
4078d94aa38SDavid Howells {
4088d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
4098d94aa38SDavid Howells 	int n = atomic_read(&call->usage);
4108d94aa38SDavid Howells 	ASSERTCMP(n, >=, 1);
4118d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
4122ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
4138d94aa38SDavid Howells 	else
4148d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4158d94aa38SDavid Howells 	return true;
4168d94aa38SDavid Howells }
4178d94aa38SDavid Howells 
4188d94aa38SDavid Howells /*
419e34d4234SDavid Howells  * Note the re-emergence of a call.
420e34d4234SDavid Howells  */
421e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call)
422e34d4234SDavid Howells {
423e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
424e34d4234SDavid Howells 	if (call) {
425e34d4234SDavid Howells 		int n = atomic_read(&call->usage);
426e34d4234SDavid Howells 
4272ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
428e34d4234SDavid Howells 	}
429e34d4234SDavid Howells }
430e34d4234SDavid Howells 
431e34d4234SDavid Howells /*
432e34d4234SDavid Howells  * Note the addition of a ref on a call.
433e34d4234SDavid Howells  */
434fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
435e34d4234SDavid Howells {
436e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
437e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
438e34d4234SDavid Howells 
4392ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
440e34d4234SDavid Howells }
441e34d4234SDavid Howells 
442e34d4234SDavid Howells /*
443248f219cSDavid Howells  * Detach a call from its owning socket.
4448c3e34a4SDavid Howells  */
4458d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
4468c3e34a4SDavid Howells {
447a84a46d7SDavid Howells 	const void *here = __builtin_return_address(0);
448248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
449248f219cSDavid Howells 	bool put = false;
450248f219cSDavid Howells 	int i;
451248f219cSDavid Howells 
452248f219cSDavid Howells 	_enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
453248f219cSDavid Howells 
454a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
455a84a46d7SDavid Howells 			 here, (const void *)call->flags);
4568c3e34a4SDavid Howells 
457a84a46d7SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
458e34d4234SDavid Howells 
4598c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4608c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4618c3e34a4SDavid Howells 		BUG();
4628c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4638c3e34a4SDavid Howells 
464248f219cSDavid Howells 	del_timer_sync(&call->timer);
4658c3e34a4SDavid Howells 
466248f219cSDavid Howells 	/* Make sure we don't get any more notifications */
467248f219cSDavid Howells 	write_lock_bh(&rx->recvmsg_lock);
468e653cfe4SDavid Howells 
469248f219cSDavid Howells 	if (!list_empty(&call->recvmsg_link)) {
4708c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4718c3e34a4SDavid Howells 		       call, call->events, call->flags);
472248f219cSDavid Howells 		list_del(&call->recvmsg_link);
473248f219cSDavid Howells 		put = true;
474248f219cSDavid Howells 	}
475248f219cSDavid Howells 
476248f219cSDavid Howells 	/* list_empty() must return false in rxrpc_notify_socket() */
477248f219cSDavid Howells 	call->recvmsg_link.next = NULL;
478248f219cSDavid Howells 	call->recvmsg_link.prev = NULL;
479248f219cSDavid Howells 
480248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
481248f219cSDavid Howells 	if (put)
482248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
483248f219cSDavid Howells 
484248f219cSDavid Howells 	write_lock(&rx->call_lock);
485248f219cSDavid Howells 
486248f219cSDavid Howells 	if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4878c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4888c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4898d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_userid);
4908c3e34a4SDavid Howells 	}
4918c3e34a4SDavid Howells 
492248f219cSDavid Howells 	list_del(&call->sock_link);
493248f219cSDavid Howells 	write_unlock(&rx->call_lock);
4948c3e34a4SDavid Howells 
495248f219cSDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4968c3e34a4SDavid Howells 
497248f219cSDavid Howells 	if (conn)
498e653cfe4SDavid Howells 		rxrpc_disconnect_call(call);
499e653cfe4SDavid Howells 
500248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
50171f3ca40SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i],
50271f3ca40SDavid Howells 			       (call->tx_phase ? rxrpc_skb_tx_cleaned :
50371f3ca40SDavid Howells 				rxrpc_skb_rx_cleaned));
504248f219cSDavid Howells 		call->rxtx_buffer[i] = NULL;
5058c3e34a4SDavid Howells 	}
5068c3e34a4SDavid Howells 
5078c3e34a4SDavid Howells 	_leave("");
5088c3e34a4SDavid Howells }
5098c3e34a4SDavid Howells 
5108c3e34a4SDavid Howells /*
511c038a58cSDavid Howells  * Prepare a kernel service call for retry.
512c038a58cSDavid Howells  */
513c038a58cSDavid Howells int rxrpc_prepare_call_for_retry(struct rxrpc_sock *rx, struct rxrpc_call *call)
514c038a58cSDavid Howells {
515c038a58cSDavid Howells 	const void *here = __builtin_return_address(0);
516c038a58cSDavid Howells 	int i;
517c038a58cSDavid Howells 	u8 last = 0;
518c038a58cSDavid Howells 
519c038a58cSDavid Howells 	_enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
520c038a58cSDavid Howells 
521c038a58cSDavid Howells 	trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
522c038a58cSDavid Howells 			 here, (const void *)call->flags);
523c038a58cSDavid Howells 
524c038a58cSDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
525c038a58cSDavid Howells 	ASSERTCMP(call->completion, !=, RXRPC_CALL_REMOTELY_ABORTED);
526c038a58cSDavid Howells 	ASSERTCMP(call->completion, !=, RXRPC_CALL_LOCALLY_ABORTED);
527c038a58cSDavid Howells 	ASSERT(list_empty(&call->recvmsg_link));
528c038a58cSDavid Howells 
529c038a58cSDavid Howells 	del_timer_sync(&call->timer);
530c038a58cSDavid Howells 
531c038a58cSDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, call->conn);
532c038a58cSDavid Howells 
533c038a58cSDavid Howells 	if (call->conn)
534c038a58cSDavid Howells 		rxrpc_disconnect_call(call);
535c038a58cSDavid Howells 
536c038a58cSDavid Howells 	if (rxrpc_is_service_call(call) ||
537c038a58cSDavid Howells 	    !call->tx_phase ||
538c038a58cSDavid Howells 	    call->tx_hard_ack != 0 ||
539c038a58cSDavid Howells 	    call->rx_hard_ack != 0 ||
540c038a58cSDavid Howells 	    call->rx_top != 0)
541c038a58cSDavid Howells 		return -EINVAL;
542c038a58cSDavid Howells 
543c038a58cSDavid Howells 	call->state = RXRPC_CALL_UNINITIALISED;
544c038a58cSDavid Howells 	call->completion = RXRPC_CALL_SUCCEEDED;
545c038a58cSDavid Howells 	call->call_id = 0;
546c038a58cSDavid Howells 	call->cid = 0;
547c038a58cSDavid Howells 	call->cong_cwnd = 0;
548c038a58cSDavid Howells 	call->cong_extra = 0;
549c038a58cSDavid Howells 	call->cong_ssthresh = 0;
550c038a58cSDavid Howells 	call->cong_mode = 0;
551c038a58cSDavid Howells 	call->cong_dup_acks = 0;
552c038a58cSDavid Howells 	call->cong_cumul_acks = 0;
553c038a58cSDavid Howells 	call->acks_lowest_nak = 0;
554c038a58cSDavid Howells 
555c038a58cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
556c038a58cSDavid Howells 		last |= call->rxtx_annotations[i];
557c038a58cSDavid Howells 		call->rxtx_annotations[i] &= RXRPC_TX_ANNO_LAST;
558c038a58cSDavid Howells 		call->rxtx_annotations[i] |= RXRPC_TX_ANNO_RETRANS;
559c038a58cSDavid Howells 	}
560c038a58cSDavid Howells 
561c038a58cSDavid Howells 	_leave(" = 0");
562c038a58cSDavid Howells 	return 0;
563c038a58cSDavid Howells }
564c038a58cSDavid Howells 
565c038a58cSDavid Howells /*
5668c3e34a4SDavid Howells  * release all the calls associated with a socket
5678c3e34a4SDavid Howells  */
5688c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
5698c3e34a4SDavid Howells {
5708c3e34a4SDavid Howells 	struct rxrpc_call *call;
5718c3e34a4SDavid Howells 
5728c3e34a4SDavid Howells 	_enter("%p", rx);
5738c3e34a4SDavid Howells 
5740360da6dSDavid Howells 	while (!list_empty(&rx->to_be_accepted)) {
5750360da6dSDavid Howells 		call = list_entry(rx->to_be_accepted.next,
5760360da6dSDavid Howells 				  struct rxrpc_call, accept_link);
5770360da6dSDavid Howells 		list_del(&call->accept_link);
5783a92789aSDavid Howells 		rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
5790360da6dSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5800360da6dSDavid Howells 	}
5810360da6dSDavid Howells 
582248f219cSDavid Howells 	while (!list_empty(&rx->sock_calls)) {
583248f219cSDavid Howells 		call = list_entry(rx->sock_calls.next,
584248f219cSDavid Howells 				  struct rxrpc_call, sock_link);
585248f219cSDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
5863a92789aSDavid Howells 		rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
58726cb02aaSDavid Howells 		rxrpc_send_abort_packet(call);
5888d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
589248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5908c3e34a4SDavid Howells 	}
5918c3e34a4SDavid Howells 
5928c3e34a4SDavid Howells 	_leave("");
5938c3e34a4SDavid Howells }
5948c3e34a4SDavid Howells 
5958c3e34a4SDavid Howells /*
5968c3e34a4SDavid Howells  * release a call
5978c3e34a4SDavid Howells  */
598fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
5998c3e34a4SDavid Howells {
6002baec2c3SDavid Howells 	struct rxrpc_net *rxnet;
601e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
6022ab27215SDavid Howells 	int n;
603e34d4234SDavid Howells 
6048c3e34a4SDavid Howells 	ASSERT(call != NULL);
6058c3e34a4SDavid Howells 
606e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
6072ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
608e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
609e34d4234SDavid Howells 	if (n == 0) {
6108c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
611248f219cSDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
612e34d4234SDavid Howells 
6132baec2c3SDavid Howells 		if (!list_empty(&call->link)) {
6142baec2c3SDavid Howells 			rxnet = rxrpc_net(sock_net(&call->socket->sk));
6152baec2c3SDavid Howells 			write_lock(&rxnet->call_lock);
616248f219cSDavid Howells 			list_del_init(&call->link);
6172baec2c3SDavid Howells 			write_unlock(&rxnet->call_lock);
6182baec2c3SDavid Howells 		}
619e34d4234SDavid Howells 
6208d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
621e34d4234SDavid Howells 	}
6228c3e34a4SDavid Howells }
6238c3e34a4SDavid Howells 
6248c3e34a4SDavid Howells /*
625dee46364SDavid Howells  * Final call destruction under RCU.
626dee46364SDavid Howells  */
627dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
628dee46364SDavid Howells {
629dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
630dee46364SDavid Howells 
631df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
632248f219cSDavid Howells 	kfree(call->rxtx_buffer);
633248f219cSDavid Howells 	kfree(call->rxtx_annotations);
634dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
635dee46364SDavid Howells }
636dee46364SDavid Howells 
637dee46364SDavid Howells /*
6388c3e34a4SDavid Howells  * clean up a call
6398c3e34a4SDavid Howells  */
64000e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call)
6418c3e34a4SDavid Howells {
642248f219cSDavid Howells 	int i;
6438c3e34a4SDavid Howells 
644248f219cSDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
6458c3e34a4SDavid Howells 
6468c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
6478c3e34a4SDavid Howells 
648248f219cSDavid Howells 	del_timer_sync(&call->timer);
6498c3e34a4SDavid Howells 
6508d94aa38SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
6518c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
652e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
6538c3e34a4SDavid Howells 
654248f219cSDavid Howells 	/* Clean up the Rx/Tx buffer */
655248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
65671f3ca40SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i],
65771f3ca40SDavid Howells 			       (call->tx_phase ? rxrpc_skb_tx_cleaned :
65871f3ca40SDavid Howells 				rxrpc_skb_rx_cleaned));
6598c3e34a4SDavid Howells 
66071f3ca40SDavid Howells 	rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned);
6618c3e34a4SDavid Howells 
662dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
6638c3e34a4SDavid Howells }
6648c3e34a4SDavid Howells 
6658c3e34a4SDavid Howells /*
6662baec2c3SDavid Howells  * Make sure that all calls are gone from a network namespace.  To reach this
6672baec2c3SDavid Howells  * point, any open UDP sockets in that namespace must have been closed, so any
6682baec2c3SDavid Howells  * outstanding calls cannot be doing I/O.
6698c3e34a4SDavid Howells  */
6702baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
6718c3e34a4SDavid Howells {
6728c3e34a4SDavid Howells 	struct rxrpc_call *call;
6738c3e34a4SDavid Howells 
6748c3e34a4SDavid Howells 	_enter("");
6758d94aa38SDavid Howells 
6762baec2c3SDavid Howells 	if (list_empty(&rxnet->calls))
6778d94aa38SDavid Howells 		return;
6788d94aa38SDavid Howells 
6792baec2c3SDavid Howells 	write_lock(&rxnet->call_lock);
6808c3e34a4SDavid Howells 
6812baec2c3SDavid Howells 	while (!list_empty(&rxnet->calls)) {
6822baec2c3SDavid Howells 		call = list_entry(rxnet->calls.next, struct rxrpc_call, link);
6838c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
6848c3e34a4SDavid Howells 
685e34d4234SDavid Howells 		rxrpc_see_call(call);
6868c3e34a4SDavid Howells 		list_del_init(&call->link);
6878c3e34a4SDavid Howells 
688248f219cSDavid Howells 		pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
6898c3e34a4SDavid Howells 		       call, atomic_read(&call->usage),
6908c3e34a4SDavid Howells 		       rxrpc_call_states[call->state],
6918c3e34a4SDavid Howells 		       call->flags, call->events);
6928c3e34a4SDavid Howells 
6932baec2c3SDavid Howells 		write_unlock(&rxnet->call_lock);
6948c3e34a4SDavid Howells 		cond_resched();
6952baec2c3SDavid Howells 		write_lock(&rxnet->call_lock);
6968c3e34a4SDavid Howells 	}
6978c3e34a4SDavid Howells 
6982baec2c3SDavid Howells 	write_unlock(&rxnet->call_lock);
6998c3e34a4SDavid Howells }
700