xref: /openbmc/linux/net/rxrpc/call_object.c (revision 2baec2c3)
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 
48248f219cSDavid Howells static void rxrpc_call_timer_expired(unsigned long _call)
49248f219cSDavid Howells {
50248f219cSDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *)_call;
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);
117248f219cSDavid Howells 	setup_timer(&call->timer, rxrpc_call_timer_expired,
1188c3e34a4SDavid Howells 		    (unsigned long)call);
1198c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
120999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
12145025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1228c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
123248f219cSDavid Howells 	INIT_LIST_HEAD(&call->recvmsg_link);
124248f219cSDavid Howells 	INIT_LIST_HEAD(&call->sock_link);
12545025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1268c3e34a4SDavid Howells 	spin_lock_init(&call->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);
1308c3e34a4SDavid Howells 
1318c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1328c3e34a4SDavid Howells 
133248f219cSDavid Howells 	/* Leave space in the ring to handle a maxed-out jumbo packet */
13475e42126SDavid Howells 	call->rx_winsize = rxrpc_rx_window_size;
135248f219cSDavid Howells 	call->tx_winsize = 16;
136248f219cSDavid Howells 	call->rx_expect_next = 1;
13757494343SDavid Howells 
13857494343SDavid Howells 	if (RXRPC_TX_SMSS > 2190)
13957494343SDavid Howells 		call->cong_cwnd = 2;
14057494343SDavid Howells 	else if (RXRPC_TX_SMSS > 1095)
14157494343SDavid Howells 		call->cong_cwnd = 3;
14257494343SDavid Howells 	else
14357494343SDavid Howells 		call->cong_cwnd = 4;
14457494343SDavid Howells 	call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
1458c3e34a4SDavid Howells 	return call;
146248f219cSDavid Howells 
147248f219cSDavid Howells nomem_2:
148248f219cSDavid Howells 	kfree(call->rxtx_buffer);
149248f219cSDavid Howells nomem:
150248f219cSDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
151248f219cSDavid Howells 	return NULL;
1528c3e34a4SDavid Howells }
1538c3e34a4SDavid Howells 
1548c3e34a4SDavid Howells /*
155999b69f8SDavid Howells  * Allocate a new client call.
1568c3e34a4SDavid Howells  */
157248f219cSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct sockaddr_rxrpc *srx,
1588c3e34a4SDavid Howells 						  gfp_t gfp)
1598c3e34a4SDavid Howells {
1608c3e34a4SDavid Howells 	struct rxrpc_call *call;
16157494343SDavid Howells 	ktime_t now;
1628c3e34a4SDavid Howells 
1638c3e34a4SDavid Howells 	_enter("");
1648c3e34a4SDavid Howells 
1658c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
1668c3e34a4SDavid Howells 	if (!call)
1678c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
168999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
169999b69f8SDavid Howells 	call->service_id = srx->srx_service;
17071f3ca40SDavid Howells 	call->tx_phase = true;
17157494343SDavid Howells 	now = ktime_get_real();
17257494343SDavid Howells 	call->acks_latest_ts = now;
17357494343SDavid Howells 	call->cong_tstamp = now;
174999b69f8SDavid Howells 
175999b69f8SDavid Howells 	_leave(" = %p", call);
176999b69f8SDavid Howells 	return call;
177999b69f8SDavid Howells }
178999b69f8SDavid Howells 
179999b69f8SDavid Howells /*
180248f219cSDavid Howells  * Initiate the call ack/resend/expiry timer.
181999b69f8SDavid Howells  */
182248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call)
183999b69f8SDavid Howells {
184df0adc78SDavid Howells 	ktime_t now = ktime_get_real(), expire_at;
185999b69f8SDavid Howells 
186df0adc78SDavid Howells 	expire_at = ktime_add_ms(now, rxrpc_max_call_lifetime);
187248f219cSDavid Howells 	call->expire_at = expire_at;
188248f219cSDavid Howells 	call->ack_at = expire_at;
189a5af7e1fSDavid Howells 	call->ping_at = expire_at;
190248f219cSDavid Howells 	call->resend_at = expire_at;
191df0adc78SDavid Howells 	call->timer.expires = jiffies + LONG_MAX / 2;
192df0adc78SDavid Howells 	rxrpc_set_timer(call, rxrpc_timer_begin, now);
1938c3e34a4SDavid Howells }
1948c3e34a4SDavid Howells 
1958c3e34a4SDavid Howells /*
196540b1c48SDavid Howells  * Set up a call for the given parameters.
197540b1c48SDavid Howells  * - Called with the socket lock held, which it must release.
198540b1c48SDavid Howells  * - If it returns a call, the call's lock will need releasing by the caller.
1998c3e34a4SDavid Howells  */
2008c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
20119ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
202999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
2038c3e34a4SDavid Howells 					 unsigned long user_call_ID,
2048c3e34a4SDavid Howells 					 gfp_t gfp)
205540b1c48SDavid Howells 	__releases(&rx->sk.sk_lock.slock)
2068c3e34a4SDavid Howells {
2078c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
2082baec2c3SDavid Howells 	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
2098c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
210e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
211999b69f8SDavid Howells 	int ret;
2128c3e34a4SDavid Howells 
213999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2148c3e34a4SDavid Howells 
215248f219cSDavid Howells 	call = rxrpc_alloc_client_call(srx, gfp);
2168c3e34a4SDavid Howells 	if (IS_ERR(call)) {
217540b1c48SDavid Howells 		release_sock(&rx->sk);
2188c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2198c3e34a4SDavid Howells 		return call;
2208c3e34a4SDavid Howells 	}
2218c3e34a4SDavid Howells 
222a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage),
223a84a46d7SDavid Howells 			 here, (const void *)user_call_ID);
224e34d4234SDavid Howells 
225540b1c48SDavid Howells 	/* We need to protect a partially set up call against the user as we
226540b1c48SDavid Howells 	 * will be acting outside the socket lock.
227540b1c48SDavid Howells 	 */
228540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
229540b1c48SDavid Howells 
230999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2318c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2328c3e34a4SDavid Howells 
2338c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2348c3e34a4SDavid Howells 	parent = NULL;
2358c3e34a4SDavid Howells 	while (*pp) {
2368c3e34a4SDavid Howells 		parent = *pp;
2378c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2388c3e34a4SDavid Howells 
2398c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
2408c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
2418c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
2428c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2438c3e34a4SDavid Howells 		else
244357f5ef6SDavid Howells 			goto error_dup_user_ID;
2458c3e34a4SDavid Howells 	}
2468c3e34a4SDavid Howells 
247248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
248357f5ef6SDavid Howells 	call->user_call_ID = user_call_ID;
249357f5ef6SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
250fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got_userid);
2518c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2528c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
253248f219cSDavid Howells 	list_add(&call->sock_link, &rx->sock_calls);
254248f219cSDavid Howells 
2558c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2568c3e34a4SDavid Howells 
2572baec2c3SDavid Howells 	write_lock(&rxnet->call_lock);
2582baec2c3SDavid Howells 	list_add_tail(&call->link, &rxnet->calls);
2592baec2c3SDavid Howells 	write_unlock(&rxnet->call_lock);
2608c3e34a4SDavid Howells 
261540b1c48SDavid Howells 	/* From this point on, the call is protected by its own lock. */
262540b1c48SDavid Howells 	release_sock(&rx->sk);
263540b1c48SDavid Howells 
264248f219cSDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
265248f219cSDavid Howells 	 * including channel number and call ID.
266248f219cSDavid Howells 	 */
267248f219cSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
268999b69f8SDavid Howells 	if (ret < 0)
269999b69f8SDavid Howells 		goto error;
270999b69f8SDavid Howells 
271a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
27254fde423SDavid Howells 			 here, NULL);
273a84a46d7SDavid Howells 
274248f219cSDavid Howells 	spin_lock_bh(&call->conn->params.peer->lock);
275248f219cSDavid Howells 	hlist_add_head(&call->error_link,
276248f219cSDavid Howells 		       &call->conn->params.peer->error_targets);
277248f219cSDavid Howells 	spin_unlock_bh(&call->conn->params.peer->lock);
278248f219cSDavid Howells 
279248f219cSDavid Howells 	rxrpc_start_call_timer(call);
280248f219cSDavid Howells 
2818c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2828c3e34a4SDavid Howells 
2838c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
2848c3e34a4SDavid Howells 	return call;
2858c3e34a4SDavid Howells 
2868c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
2878c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
2888c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
2898c3e34a4SDavid Howells 	 * same time in different threads.
2908c3e34a4SDavid Howells 	 */
291357f5ef6SDavid Howells error_dup_user_ID:
2928c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
293540b1c48SDavid Howells 	release_sock(&rx->sk);
2948d94aa38SDavid Howells 	ret = -EEXIST;
295357f5ef6SDavid Howells 
296357f5ef6SDavid Howells error:
297357f5ef6SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
298357f5ef6SDavid Howells 				    RX_CALL_DEAD, ret);
299a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
300a84a46d7SDavid Howells 			 here, ERR_PTR(ret));
301357f5ef6SDavid Howells 	rxrpc_release_call(rx, call);
302540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
303357f5ef6SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
304357f5ef6SDavid Howells 	_leave(" = %d", ret);
305357f5ef6SDavid Howells 	return ERR_PTR(ret);
3068c3e34a4SDavid Howells }
3078c3e34a4SDavid Howells 
3088c3e34a4SDavid Howells /*
309248f219cSDavid Howells  * Set up an incoming call.  call->conn points to the connection.
310248f219cSDavid Howells  * This is called in BH context and isn't allowed to fail.
3118c3e34a4SDavid Howells  */
312248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx,
313248f219cSDavid Howells 			 struct rxrpc_call *call,
31442886ffeSDavid Howells 			 struct sk_buff *skb)
3158c3e34a4SDavid Howells {
316248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
31742886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
318248f219cSDavid Howells 	u32 chan;
3198c3e34a4SDavid Howells 
320248f219cSDavid Howells 	_enter(",%d", call->conn->debug_id);
3218c3e34a4SDavid Howells 
322248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
323248f219cSDavid Howells 	call->call_id		= sp->hdr.callNumber;
324248f219cSDavid Howells 	call->service_id	= sp->hdr.serviceId;
325248f219cSDavid Howells 	call->cid		= sp->hdr.cid;
326248f219cSDavid Howells 	call->state		= RXRPC_CALL_SERVER_ACCEPTING;
327248f219cSDavid Howells 	if (sp->hdr.securityIndex > 0)
328248f219cSDavid Howells 		call->state	= RXRPC_CALL_SERVER_SECURING;
32957494343SDavid Howells 	call->cong_tstamp	= skb->tstamp;
3308c3e34a4SDavid Howells 
331248f219cSDavid Howells 	/* Set the channel for this call.  We don't get channel_lock as we're
332248f219cSDavid Howells 	 * only defending against the data_ready handler (which we're called
333248f219cSDavid Howells 	 * from) and the RESPONSE packet parser (which is only really
334248f219cSDavid Howells 	 * interested in call_counter and can cope with a disagreement with the
335248f219cSDavid Howells 	 * call pointer).
3368c3e34a4SDavid Howells 	 */
337248f219cSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
338248f219cSDavid Howells 	conn->channels[chan].call_counter = call->call_id;
339248f219cSDavid Howells 	conn->channels[chan].call_id = call->call_id;
340a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3418c3e34a4SDavid Howells 
34285f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
34385f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
34485f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3458c3e34a4SDavid Howells 
3468c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
3478c3e34a4SDavid Howells 
348248f219cSDavid Howells 	rxrpc_start_call_timer(call);
349248f219cSDavid Howells 	_leave("");
3508c3e34a4SDavid Howells }
3518c3e34a4SDavid Howells 
3528c3e34a4SDavid Howells /*
3538d94aa38SDavid Howells  * Queue a call's work processor, getting a ref to pass to the work queue.
3548d94aa38SDavid Howells  */
3558d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call)
3568d94aa38SDavid Howells {
3578d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3588d94aa38SDavid Howells 	int n = __atomic_add_unless(&call->usage, 1, 0);
3598d94aa38SDavid Howells 	if (n == 0)
3608d94aa38SDavid Howells 		return false;
3618d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3622ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
3638d94aa38SDavid Howells 	else
3648d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3658d94aa38SDavid Howells 	return true;
3668d94aa38SDavid Howells }
3678d94aa38SDavid Howells 
3688d94aa38SDavid Howells /*
3698d94aa38SDavid Howells  * Queue a call's work processor, passing the callers ref to the work queue.
3708d94aa38SDavid Howells  */
3718d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call)
3728d94aa38SDavid Howells {
3738d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
3748d94aa38SDavid Howells 	int n = atomic_read(&call->usage);
3758d94aa38SDavid Howells 	ASSERTCMP(n, >=, 1);
3768d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
3772ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
3788d94aa38SDavid Howells 	else
3798d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3808d94aa38SDavid Howells 	return true;
3818d94aa38SDavid Howells }
3828d94aa38SDavid Howells 
3838d94aa38SDavid Howells /*
384e34d4234SDavid Howells  * Note the re-emergence of a call.
385e34d4234SDavid Howells  */
386e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call)
387e34d4234SDavid Howells {
388e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
389e34d4234SDavid Howells 	if (call) {
390e34d4234SDavid Howells 		int n = atomic_read(&call->usage);
391e34d4234SDavid Howells 
3922ab27215SDavid Howells 		trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
393e34d4234SDavid Howells 	}
394e34d4234SDavid Howells }
395e34d4234SDavid Howells 
396e34d4234SDavid Howells /*
397e34d4234SDavid Howells  * Note the addition of a ref on a call.
398e34d4234SDavid Howells  */
399fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
400e34d4234SDavid Howells {
401e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
402e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
403e34d4234SDavid Howells 
4042ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
405e34d4234SDavid Howells }
406e34d4234SDavid Howells 
407e34d4234SDavid Howells /*
408248f219cSDavid Howells  * Detach a call from its owning socket.
4098c3e34a4SDavid Howells  */
4108d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
4118c3e34a4SDavid Howells {
412a84a46d7SDavid Howells 	const void *here = __builtin_return_address(0);
413248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
414248f219cSDavid Howells 	bool put = false;
415248f219cSDavid Howells 	int i;
416248f219cSDavid Howells 
417248f219cSDavid Howells 	_enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
418248f219cSDavid Howells 
419a84a46d7SDavid Howells 	trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
420a84a46d7SDavid Howells 			 here, (const void *)call->flags);
4218c3e34a4SDavid Howells 
422a84a46d7SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
423e34d4234SDavid Howells 
4248c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4258c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4268c3e34a4SDavid Howells 		BUG();
4278c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4288c3e34a4SDavid Howells 
429248f219cSDavid Howells 	del_timer_sync(&call->timer);
4308c3e34a4SDavid Howells 
431248f219cSDavid Howells 	/* Make sure we don't get any more notifications */
432248f219cSDavid Howells 	write_lock_bh(&rx->recvmsg_lock);
433e653cfe4SDavid Howells 
434248f219cSDavid Howells 	if (!list_empty(&call->recvmsg_link)) {
4358c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4368c3e34a4SDavid Howells 		       call, call->events, call->flags);
437248f219cSDavid Howells 		list_del(&call->recvmsg_link);
438248f219cSDavid Howells 		put = true;
439248f219cSDavid Howells 	}
440248f219cSDavid Howells 
441248f219cSDavid Howells 	/* list_empty() must return false in rxrpc_notify_socket() */
442248f219cSDavid Howells 	call->recvmsg_link.next = NULL;
443248f219cSDavid Howells 	call->recvmsg_link.prev = NULL;
444248f219cSDavid Howells 
445248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
446248f219cSDavid Howells 	if (put)
447248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
448248f219cSDavid Howells 
449248f219cSDavid Howells 	write_lock(&rx->call_lock);
450248f219cSDavid Howells 
451248f219cSDavid Howells 	if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4528c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4538c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4548d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_userid);
4558c3e34a4SDavid Howells 	}
4568c3e34a4SDavid Howells 
457248f219cSDavid Howells 	list_del(&call->sock_link);
458248f219cSDavid Howells 	write_unlock(&rx->call_lock);
4598c3e34a4SDavid Howells 
460248f219cSDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
4618c3e34a4SDavid Howells 
462248f219cSDavid Howells 	if (conn)
463e653cfe4SDavid Howells 		rxrpc_disconnect_call(call);
464e653cfe4SDavid Howells 
465248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
46671f3ca40SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i],
46771f3ca40SDavid Howells 			       (call->tx_phase ? rxrpc_skb_tx_cleaned :
46871f3ca40SDavid Howells 				rxrpc_skb_rx_cleaned));
469248f219cSDavid Howells 		call->rxtx_buffer[i] = NULL;
4708c3e34a4SDavid Howells 	}
4718c3e34a4SDavid Howells 
4728c3e34a4SDavid Howells 	_leave("");
4738c3e34a4SDavid Howells }
4748c3e34a4SDavid Howells 
4758c3e34a4SDavid Howells /*
4768c3e34a4SDavid Howells  * release all the calls associated with a socket
4778c3e34a4SDavid Howells  */
4788c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
4798c3e34a4SDavid Howells {
4808c3e34a4SDavid Howells 	struct rxrpc_call *call;
4818c3e34a4SDavid Howells 
4828c3e34a4SDavid Howells 	_enter("%p", rx);
4838c3e34a4SDavid Howells 
4840360da6dSDavid Howells 	while (!list_empty(&rx->to_be_accepted)) {
4850360da6dSDavid Howells 		call = list_entry(rx->to_be_accepted.next,
4860360da6dSDavid Howells 				  struct rxrpc_call, accept_link);
4870360da6dSDavid Howells 		list_del(&call->accept_link);
4883a92789aSDavid Howells 		rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
4890360da6dSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
4900360da6dSDavid Howells 	}
4910360da6dSDavid Howells 
492248f219cSDavid Howells 	while (!list_empty(&rx->sock_calls)) {
493248f219cSDavid Howells 		call = list_entry(rx->sock_calls.next,
494248f219cSDavid Howells 				  struct rxrpc_call, sock_link);
495248f219cSDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
4963a92789aSDavid Howells 		rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
49726cb02aaSDavid Howells 		rxrpc_send_abort_packet(call);
4988d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
499248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5008c3e34a4SDavid Howells 	}
5018c3e34a4SDavid Howells 
5028c3e34a4SDavid Howells 	_leave("");
5038c3e34a4SDavid Howells }
5048c3e34a4SDavid Howells 
5058c3e34a4SDavid Howells /*
5068c3e34a4SDavid Howells  * release a call
5078c3e34a4SDavid Howells  */
508fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
5098c3e34a4SDavid Howells {
5102baec2c3SDavid Howells 	struct rxrpc_net *rxnet;
511e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
5122ab27215SDavid Howells 	int n;
513e34d4234SDavid Howells 
5148c3e34a4SDavid Howells 	ASSERT(call != NULL);
5158c3e34a4SDavid Howells 
516e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
5172ab27215SDavid Howells 	trace_rxrpc_call(call, op, n, here, NULL);
518e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
519e34d4234SDavid Howells 	if (n == 0) {
5208c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
521248f219cSDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
522e34d4234SDavid Howells 
5232baec2c3SDavid Howells 		if (!list_empty(&call->link)) {
5242baec2c3SDavid Howells 			rxnet = rxrpc_net(sock_net(&call->socket->sk));
5252baec2c3SDavid Howells 			write_lock(&rxnet->call_lock);
526248f219cSDavid Howells 			list_del_init(&call->link);
5272baec2c3SDavid Howells 			write_unlock(&rxnet->call_lock);
5282baec2c3SDavid Howells 		}
529e34d4234SDavid Howells 
5308d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
531e34d4234SDavid Howells 	}
5328c3e34a4SDavid Howells }
5338c3e34a4SDavid Howells 
5348c3e34a4SDavid Howells /*
535dee46364SDavid Howells  * Final call destruction under RCU.
536dee46364SDavid Howells  */
537dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
538dee46364SDavid Howells {
539dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
540dee46364SDavid Howells 
541df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
542248f219cSDavid Howells 	kfree(call->rxtx_buffer);
543248f219cSDavid Howells 	kfree(call->rxtx_annotations);
544dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
545dee46364SDavid Howells }
546dee46364SDavid Howells 
547dee46364SDavid Howells /*
5488c3e34a4SDavid Howells  * clean up a call
5498c3e34a4SDavid Howells  */
55000e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call)
5518c3e34a4SDavid Howells {
552248f219cSDavid Howells 	int i;
5538c3e34a4SDavid Howells 
554248f219cSDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
5558c3e34a4SDavid Howells 
5568c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
5578c3e34a4SDavid Howells 
558248f219cSDavid Howells 	del_timer_sync(&call->timer);
5598c3e34a4SDavid Howells 
5608d94aa38SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
5618c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
562e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
5638c3e34a4SDavid Howells 
564248f219cSDavid Howells 	/* Clean up the Rx/Tx buffer */
565248f219cSDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
56671f3ca40SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i],
56771f3ca40SDavid Howells 			       (call->tx_phase ? rxrpc_skb_tx_cleaned :
56871f3ca40SDavid Howells 				rxrpc_skb_rx_cleaned));
5698c3e34a4SDavid Howells 
57071f3ca40SDavid Howells 	rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned);
5718c3e34a4SDavid Howells 
572dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
5738c3e34a4SDavid Howells }
5748c3e34a4SDavid Howells 
5758c3e34a4SDavid Howells /*
5762baec2c3SDavid Howells  * Make sure that all calls are gone from a network namespace.  To reach this
5772baec2c3SDavid Howells  * point, any open UDP sockets in that namespace must have been closed, so any
5782baec2c3SDavid Howells  * outstanding calls cannot be doing I/O.
5798c3e34a4SDavid Howells  */
5802baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
5818c3e34a4SDavid Howells {
5828c3e34a4SDavid Howells 	struct rxrpc_call *call;
5838c3e34a4SDavid Howells 
5848c3e34a4SDavid Howells 	_enter("");
5858d94aa38SDavid Howells 
5862baec2c3SDavid Howells 	if (list_empty(&rxnet->calls))
5878d94aa38SDavid Howells 		return;
5888d94aa38SDavid Howells 
5892baec2c3SDavid Howells 	write_lock(&rxnet->call_lock);
5908c3e34a4SDavid Howells 
5912baec2c3SDavid Howells 	while (!list_empty(&rxnet->calls)) {
5922baec2c3SDavid Howells 		call = list_entry(rxnet->calls.next, struct rxrpc_call, link);
5938c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
5948c3e34a4SDavid Howells 
595e34d4234SDavid Howells 		rxrpc_see_call(call);
5968c3e34a4SDavid Howells 		list_del_init(&call->link);
5978c3e34a4SDavid Howells 
598248f219cSDavid Howells 		pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
5998c3e34a4SDavid Howells 		       call, atomic_read(&call->usage),
6008c3e34a4SDavid Howells 		       rxrpc_call_states[call->state],
6018c3e34a4SDavid Howells 		       call->flags, call->events);
6028c3e34a4SDavid Howells 
6032baec2c3SDavid Howells 		write_unlock(&rxnet->call_lock);
6048c3e34a4SDavid Howells 		cond_resched();
6052baec2c3SDavid Howells 		write_lock(&rxnet->call_lock);
6068c3e34a4SDavid Howells 	}
6078c3e34a4SDavid Howells 
6082baec2c3SDavid Howells 	write_unlock(&rxnet->call_lock);
6098c3e34a4SDavid Howells }
610