xref: /openbmc/linux/net/rxrpc/call_object.c (revision 65550098)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling
38c3e34a4SDavid Howells  *
48c3e34a4SDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells  */
78c3e34a4SDavid Howells 
88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c3e34a4SDavid Howells 
108c3e34a4SDavid Howells #include <linux/slab.h>
118c3e34a4SDavid Howells #include <linux/module.h>
128c3e34a4SDavid Howells #include <linux/circ_buf.h>
138c3e34a4SDavid Howells #include <linux/spinlock_types.h>
148c3e34a4SDavid Howells #include <net/sock.h>
158c3e34a4SDavid Howells #include <net/af_rxrpc.h>
168c3e34a4SDavid Howells #include "ar-internal.h"
178c3e34a4SDavid Howells 
188c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
19999b69f8SDavid Howells 	[RXRPC_CALL_UNINITIALISED]		= "Uninit  ",
20999b69f8SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_CONN]		= "ClWtConn",
218c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_SEND_REQUEST]	= "ClSndReq",
228c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_REPLY]		= "ClAwtRpl",
238c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_RECV_REPLY]		= "ClRcvRpl",
2400e90712SDavid Howells 	[RXRPC_CALL_SERVER_PREALLOC]		= "SvPrealc",
258c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SECURING]		= "SvSecure",
268c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACCEPTING]		= "SvAccept",
278c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_RECV_REQUEST]	= "SvRcvReq",
288c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACK_REQUEST]		= "SvAckReq",
298c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SEND_REPLY]		= "SvSndRpl",
308c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_AWAIT_ACK]		= "SvAwtACK",
318c3e34a4SDavid Howells 	[RXRPC_CALL_COMPLETE]			= "Complete",
32f5c17aaeSDavid Howells };
33f5c17aaeSDavid Howells 
34f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
35f5c17aaeSDavid Howells 	[RXRPC_CALL_SUCCEEDED]			= "Complete",
368c3e34a4SDavid Howells 	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
378c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
38f5c17aaeSDavid Howells 	[RXRPC_CALL_LOCAL_ERROR]		= "LocError",
398c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
408c3e34a4SDavid Howells };
418c3e34a4SDavid Howells 
428c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
438c3e34a4SDavid Howells 
44e99e88a9SKees Cook static void rxrpc_call_timer_expired(struct timer_list *t)
45248f219cSDavid Howells {
46e99e88a9SKees Cook 	struct rxrpc_call *call = from_timer(call, t, timer);
47248f219cSDavid Howells 
48248f219cSDavid Howells 	_enter("%d", call->debug_id);
49248f219cSDavid Howells 
50a158bdd3SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
51a158bdd3SDavid Howells 		trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies);
52a158bdd3SDavid Howells 		rxrpc_queue_call(call);
53fc7ab6d2SDavid Howells 	}
54248f219cSDavid Howells }
558c3e34a4SDavid Howells 
569faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key;
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  */
98a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp,
99a25e21f0SDavid Howells 				    unsigned int debug_id)
1008c3e34a4SDavid Howells {
1018c3e34a4SDavid Howells 	struct rxrpc_call *call;
102d3be4d24SDavid Howells 	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
1038c3e34a4SDavid Howells 
1048c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1058c3e34a4SDavid Howells 	if (!call)
1068c3e34a4SDavid Howells 		return NULL;
1078c3e34a4SDavid Howells 
108248f219cSDavid Howells 	call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
109248f219cSDavid Howells 				    sizeof(struct sk_buff *),
1108c3e34a4SDavid Howells 				    gfp);
111248f219cSDavid Howells 	if (!call->rxtx_buffer)
112248f219cSDavid Howells 		goto nomem;
1138c3e34a4SDavid Howells 
114248f219cSDavid Howells 	call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
115248f219cSDavid Howells 	if (!call->rxtx_annotations)
116248f219cSDavid Howells 		goto nomem_2;
117248f219cSDavid Howells 
118540b1c48SDavid Howells 	mutex_init(&call->user_mutex);
1199faaff59SDavid Howells 
1209faaff59SDavid Howells 	/* Prevent lockdep reporting a deadlock false positive between the afs
1219faaff59SDavid Howells 	 * filesystem and sys_sendmsg() via the mmap sem.
1229faaff59SDavid Howells 	 */
1239faaff59SDavid Howells 	if (rx->sk.sk_kern_sock)
1249faaff59SDavid Howells 		lockdep_set_class(&call->user_mutex,
1259faaff59SDavid Howells 				  &rxrpc_call_user_mutex_lock_class_key);
1269faaff59SDavid Howells 
127e99e88a9SKees Cook 	timer_setup(&call->timer, rxrpc_call_timer_expired, 0);
1288c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
129999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
13045025bceSDavid Howells 	INIT_LIST_HEAD(&call->chan_wait_link);
1318c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
132248f219cSDavid Howells 	INIT_LIST_HEAD(&call->recvmsg_link);
133248f219cSDavid Howells 	INIT_LIST_HEAD(&call->sock_link);
13445025bceSDavid Howells 	init_waitqueue_head(&call->waitq);
1358c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
13620acbd9aSDavid Howells 	spin_lock_init(&call->notify_lock);
137c1e15b49SDavid Howells 	spin_lock_init(&call->input_lock);
1388c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
1398c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
140a25e21f0SDavid Howells 	call->debug_id = debug_id;
141e754eba6SDavid Howells 	call->tx_total_len = -1;
142a158bdd3SDavid Howells 	call->next_rx_timo = 20 * HZ;
143a158bdd3SDavid Howells 	call->next_req_timo = 1 * HZ;
1448c3e34a4SDavid Howells 
1458c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1468c3e34a4SDavid Howells 
147248f219cSDavid Howells 	/* Leave space in the ring to handle a maxed-out jumbo packet */
14875e42126SDavid Howells 	call->rx_winsize = rxrpc_rx_window_size;
149248f219cSDavid Howells 	call->tx_winsize = 16;
150248f219cSDavid Howells 	call->rx_expect_next = 1;
15157494343SDavid Howells 
15257494343SDavid Howells 	call->cong_cwnd = 2;
15357494343SDavid Howells 	call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
154d3be4d24SDavid Howells 
155d3be4d24SDavid Howells 	call->rxnet = rxnet;
156d3be4d24SDavid Howells 	atomic_inc(&rxnet->nr_calls);
1578c3e34a4SDavid Howells 	return call;
158248f219cSDavid Howells 
159248f219cSDavid Howells nomem_2:
160248f219cSDavid Howells 	kfree(call->rxtx_buffer);
161248f219cSDavid Howells nomem:
162248f219cSDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
163248f219cSDavid Howells 	return NULL;
1648c3e34a4SDavid Howells }
1658c3e34a4SDavid Howells 
1668c3e34a4SDavid Howells /*
167999b69f8SDavid Howells  * Allocate a new client call.
1688c3e34a4SDavid Howells  */
1699faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
1709faaff59SDavid Howells 						  struct sockaddr_rxrpc *srx,
171a25e21f0SDavid Howells 						  gfp_t gfp,
172a25e21f0SDavid Howells 						  unsigned int debug_id)
1738c3e34a4SDavid Howells {
1748c3e34a4SDavid Howells 	struct rxrpc_call *call;
17557494343SDavid Howells 	ktime_t now;
1768c3e34a4SDavid Howells 
1778c3e34a4SDavid Howells 	_enter("");
1788c3e34a4SDavid Howells 
179a25e21f0SDavid Howells 	call = rxrpc_alloc_call(rx, gfp, debug_id);
1808c3e34a4SDavid Howells 	if (!call)
1818c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
182999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
183999b69f8SDavid Howells 	call->service_id = srx->srx_service;
18471f3ca40SDavid Howells 	call->tx_phase = true;
18557494343SDavid Howells 	now = ktime_get_real();
18657494343SDavid Howells 	call->acks_latest_ts = now;
18757494343SDavid Howells 	call->cong_tstamp = now;
188999b69f8SDavid Howells 
189999b69f8SDavid Howells 	_leave(" = %p", call);
190999b69f8SDavid Howells 	return call;
191999b69f8SDavid Howells }
192999b69f8SDavid Howells 
193999b69f8SDavid Howells /*
194248f219cSDavid Howells  * Initiate the call ack/resend/expiry timer.
195999b69f8SDavid Howells  */
196248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call)
197999b69f8SDavid Howells {
198a158bdd3SDavid Howells 	unsigned long now = jiffies;
199a158bdd3SDavid Howells 	unsigned long j = now + MAX_JIFFY_OFFSET;
200999b69f8SDavid Howells 
201a158bdd3SDavid Howells 	call->ack_at = j;
202bd1fdf8cSDavid Howells 	call->ack_lost_at = j;
203a158bdd3SDavid Howells 	call->resend_at = j;
204a158bdd3SDavid Howells 	call->ping_at = j;
205a158bdd3SDavid Howells 	call->expect_rx_by = j;
206a158bdd3SDavid Howells 	call->expect_req_by = j;
207a158bdd3SDavid Howells 	call->expect_term_by = j;
208a158bdd3SDavid Howells 	call->timer.expires = now;
2098c3e34a4SDavid Howells }
2108c3e34a4SDavid Howells 
2118c3e34a4SDavid Howells /*
212540b1c48SDavid Howells  * Set up a call for the given parameters.
213540b1c48SDavid Howells  * - Called with the socket lock held, which it must release.
214540b1c48SDavid Howells  * - If it returns a call, the call's lock will need releasing by the caller.
2158c3e34a4SDavid Howells  */
2168c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
21719ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
218999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
21948124178SDavid Howells 					 struct rxrpc_call_params *p,
220a25e21f0SDavid Howells 					 gfp_t gfp,
221a25e21f0SDavid Howells 					 unsigned int debug_id)
222540b1c48SDavid Howells 	__releases(&rx->sk.sk_lock.slock)
22388f2a825SDavid Howells 	__acquires(&call->user_mutex)
2248c3e34a4SDavid Howells {
2258c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
226d3be4d24SDavid Howells 	struct rxrpc_net *rxnet;
2278c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
228e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
229999b69f8SDavid Howells 	int ret;
2308c3e34a4SDavid Howells 
23148124178SDavid Howells 	_enter("%p,%lx", rx, p->user_call_ID);
2328c3e34a4SDavid Howells 
233a25e21f0SDavid Howells 	call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id);
2348c3e34a4SDavid Howells 	if (IS_ERR(call)) {
235540b1c48SDavid Howells 		release_sock(&rx->sk);
2368c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
2378c3e34a4SDavid Howells 		return call;
2388c3e34a4SDavid Howells 	}
2398c3e34a4SDavid Howells 
240e138aa7dSDavid Howells 	call->interruptibility = p->interruptibility;
24148124178SDavid Howells 	call->tx_total_len = p->tx_total_len;
24248c9e0ecSDavid Howells 	trace_rxrpc_call(call->debug_id, rxrpc_call_new_client,
24348c9e0ecSDavid Howells 			 atomic_read(&call->usage),
24448124178SDavid Howells 			 here, (const void *)p->user_call_ID);
245e34d4234SDavid Howells 
246540b1c48SDavid Howells 	/* We need to protect a partially set up call against the user as we
247540b1c48SDavid Howells 	 * will be acting outside the socket lock.
248540b1c48SDavid Howells 	 */
249540b1c48SDavid Howells 	mutex_lock(&call->user_mutex);
250540b1c48SDavid Howells 
251999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
2528c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
2538c3e34a4SDavid Howells 
2548c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
2558c3e34a4SDavid Howells 	parent = NULL;
2568c3e34a4SDavid Howells 	while (*pp) {
2578c3e34a4SDavid Howells 		parent = *pp;
2588c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
2598c3e34a4SDavid Howells 
26048124178SDavid Howells 		if (p->user_call_ID < xcall->user_call_ID)
2618c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
26248124178SDavid Howells 		else if (p->user_call_ID > xcall->user_call_ID)
2638c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
2648c3e34a4SDavid Howells 		else
265357f5ef6SDavid Howells 			goto error_dup_user_ID;
2668c3e34a4SDavid Howells 	}
2678c3e34a4SDavid Howells 
268248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
26948124178SDavid Howells 	call->user_call_ID = p->user_call_ID;
270357f5ef6SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
271fff72429SDavid Howells 	rxrpc_get_call(call, rxrpc_call_got_userid);
2728c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
2738c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
274248f219cSDavid Howells 	list_add(&call->sock_link, &rx->sock_calls);
275248f219cSDavid Howells 
2768c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
2778c3e34a4SDavid Howells 
278d3be4d24SDavid Howells 	rxnet = call->rxnet;
2792baec2c3SDavid Howells 	write_lock(&rxnet->call_lock);
2802baec2c3SDavid Howells 	list_add_tail(&call->link, &rxnet->calls);
2812baec2c3SDavid Howells 	write_unlock(&rxnet->call_lock);
2828c3e34a4SDavid Howells 
283540b1c48SDavid Howells 	/* From this point on, the call is protected by its own lock. */
284540b1c48SDavid Howells 	release_sock(&rx->sk);
285540b1c48SDavid Howells 
286248f219cSDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
287248f219cSDavid Howells 	 * including channel number and call ID.
288248f219cSDavid Howells 	 */
2895e33a23bSDavid Howells 	ret = rxrpc_connect_call(rx, call, cp, srx, gfp);
290999b69f8SDavid Howells 	if (ret < 0)
29165550098SDavid Howells 		goto error_attached_to_socket;
292999b69f8SDavid Howells 
29348c9e0ecSDavid Howells 	trace_rxrpc_call(call->debug_id, rxrpc_call_connected,
29448c9e0ecSDavid Howells 			 atomic_read(&call->usage), here, NULL);
295a84a46d7SDavid Howells 
296248f219cSDavid Howells 	rxrpc_start_call_timer(call);
297248f219cSDavid Howells 
2988c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
2998c3e34a4SDavid Howells 
3008c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
3018c3e34a4SDavid Howells 	return call;
3028c3e34a4SDavid Howells 
3038c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
3048c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
3058c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
3068c3e34a4SDavid Howells 	 * same time in different threads.
3078c3e34a4SDavid Howells 	 */
308357f5ef6SDavid Howells error_dup_user_ID:
3098c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
310540b1c48SDavid Howells 	release_sock(&rx->sk);
311357f5ef6SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
31265550098SDavid Howells 				    RX_CALL_DEAD, -EEXIST);
31348c9e0ecSDavid Howells 	trace_rxrpc_call(call->debug_id, rxrpc_call_error,
31465550098SDavid Howells 			 atomic_read(&call->usage), here, ERR_PTR(-EEXIST));
315357f5ef6SDavid Howells 	rxrpc_release_call(rx, call);
316540b1c48SDavid Howells 	mutex_unlock(&call->user_mutex);
317357f5ef6SDavid Howells 	rxrpc_put_call(call, rxrpc_call_put);
31865550098SDavid Howells 	_leave(" = -EEXIST");
31965550098SDavid Howells 	return ERR_PTR(-EEXIST);
32065550098SDavid Howells 
32165550098SDavid Howells 	/* We got an error, but the call is attached to the socket and is in
32265550098SDavid Howells 	 * need of release.  However, we might now race with recvmsg() when
32365550098SDavid Howells 	 * completing the call queues it.  Return 0 from sys_sendmsg() and
32465550098SDavid Howells 	 * leave the error to recvmsg() to deal with.
32565550098SDavid Howells 	 */
32665550098SDavid Howells error_attached_to_socket:
32765550098SDavid Howells 	trace_rxrpc_call(call->debug_id, rxrpc_call_error,
32865550098SDavid Howells 			 atomic_read(&call->usage), here, ERR_PTR(ret));
32965550098SDavid Howells 	set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
33065550098SDavid Howells 	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
33165550098SDavid Howells 				    RX_CALL_DEAD, ret);
33265550098SDavid Howells 	_leave(" = c=%08x [err]", call->debug_id);
33365550098SDavid Howells 	return call;
3348c3e34a4SDavid Howells }
3358c3e34a4SDavid Howells 
3368c3e34a4SDavid Howells /*
337248f219cSDavid Howells  * Set up an incoming call.  call->conn points to the connection.
338248f219cSDavid Howells  * This is called in BH context and isn't allowed to fail.
3398c3e34a4SDavid Howells  */
340248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx,
341248f219cSDavid Howells 			 struct rxrpc_call *call,
34242886ffeSDavid Howells 			 struct sk_buff *skb)
3438c3e34a4SDavid Howells {
344248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
34542886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
346248f219cSDavid Howells 	u32 chan;
3478c3e34a4SDavid Howells 
348248f219cSDavid Howells 	_enter(",%d", call->conn->debug_id);
3498c3e34a4SDavid Howells 
350248f219cSDavid Howells 	rcu_assign_pointer(call->socket, rx);
351248f219cSDavid Howells 	call->call_id		= sp->hdr.callNumber;
352248f219cSDavid Howells 	call->service_id	= sp->hdr.serviceId;
353248f219cSDavid Howells 	call->cid		= sp->hdr.cid;
354248f219cSDavid Howells 	call->state		= RXRPC_CALL_SERVER_ACCEPTING;
355248f219cSDavid Howells 	if (sp->hdr.securityIndex > 0)
356248f219cSDavid Howells 		call->state	= RXRPC_CALL_SERVER_SECURING;
35757494343SDavid Howells 	call->cong_tstamp	= skb->tstamp;
3588c3e34a4SDavid Howells 
359248f219cSDavid Howells 	/* Set the channel for this call.  We don't get channel_lock as we're
360248f219cSDavid Howells 	 * only defending against the data_ready handler (which we're called
361248f219cSDavid Howells 	 * from) and the RESPONSE packet parser (which is only really
362248f219cSDavid Howells 	 * interested in call_counter and can cope with a disagreement with the
363248f219cSDavid Howells 	 * call pointer).
3648c3e34a4SDavid Howells 	 */
365248f219cSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
366248f219cSDavid Howells 	conn->channels[chan].call_counter = call->call_id;
367248f219cSDavid Howells 	conn->channels[chan].call_id = call->call_id;
368a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
3698c3e34a4SDavid Howells 
37085f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
371f3344303SDavid Howells 	hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets);
37285f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
3738c3e34a4SDavid Howells 
3748c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
3758c3e34a4SDavid Howells 
376248f219cSDavid Howells 	rxrpc_start_call_timer(call);
377248f219cSDavid Howells 	_leave("");
3788c3e34a4SDavid Howells }
3798c3e34a4SDavid Howells 
3808c3e34a4SDavid Howells /*
3818d94aa38SDavid Howells  * Queue a call's work processor, getting a ref to pass to the work queue.
3828d94aa38SDavid Howells  */
3838d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call)
3848d94aa38SDavid Howells {
3858d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
386bfc18e38SMark Rutland 	int n = atomic_fetch_add_unless(&call->usage, 1, 0);
3878d94aa38SDavid Howells 	if (n == 0)
3888d94aa38SDavid Howells 		return false;
3898d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
39048c9e0ecSDavid Howells 		trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1,
39148c9e0ecSDavid Howells 				 here, NULL);
3928d94aa38SDavid Howells 	else
3938d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
3948d94aa38SDavid Howells 	return true;
3958d94aa38SDavid Howells }
3968d94aa38SDavid Howells 
3978d94aa38SDavid Howells /*
3988d94aa38SDavid Howells  * Queue a call's work processor, passing the callers ref to the work queue.
3998d94aa38SDavid Howells  */
4008d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call)
4018d94aa38SDavid Howells {
4028d94aa38SDavid Howells 	const void *here = __builtin_return_address(0);
4038d94aa38SDavid Howells 	int n = atomic_read(&call->usage);
4048d94aa38SDavid Howells 	ASSERTCMP(n, >=, 1);
4058d94aa38SDavid Howells 	if (rxrpc_queue_work(&call->processor))
40648c9e0ecSDavid Howells 		trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n,
40748c9e0ecSDavid Howells 				 here, NULL);
4088d94aa38SDavid Howells 	else
4098d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4108d94aa38SDavid Howells 	return true;
4118d94aa38SDavid Howells }
4128d94aa38SDavid Howells 
4138d94aa38SDavid Howells /*
414e34d4234SDavid Howells  * Note the re-emergence of a call.
415e34d4234SDavid Howells  */
416e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call)
417e34d4234SDavid Howells {
418e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
419e34d4234SDavid Howells 	if (call) {
420e34d4234SDavid Howells 		int n = atomic_read(&call->usage);
421e34d4234SDavid Howells 
42248c9e0ecSDavid Howells 		trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n,
42348c9e0ecSDavid Howells 				 here, NULL);
424e34d4234SDavid Howells 	}
425e34d4234SDavid Howells }
426e34d4234SDavid Howells 
427e34d4234SDavid Howells /*
428e34d4234SDavid Howells  * Note the addition of a ref on a call.
429e34d4234SDavid Howells  */
430fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
431e34d4234SDavid Howells {
432e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
433e34d4234SDavid Howells 	int n = atomic_inc_return(&call->usage);
434e34d4234SDavid Howells 
43548c9e0ecSDavid Howells 	trace_rxrpc_call(call->debug_id, op, n, here, NULL);
436e34d4234SDavid Howells }
437e34d4234SDavid Howells 
438e34d4234SDavid Howells /*
439a641fd00SDavid Howells  * Clean up the RxTx skb ring.
440a641fd00SDavid Howells  */
441a641fd00SDavid Howells static void rxrpc_cleanup_ring(struct rxrpc_call *call)
442a641fd00SDavid Howells {
443a641fd00SDavid Howells 	int i;
444a641fd00SDavid Howells 
445a641fd00SDavid Howells 	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
446987db9f7SDavid Howells 		rxrpc_free_skb(call->rxtx_buffer[i], rxrpc_skb_cleaned);
447a641fd00SDavid Howells 		call->rxtx_buffer[i] = NULL;
448a641fd00SDavid Howells 	}
449a641fd00SDavid Howells }
450a641fd00SDavid Howells 
451a641fd00SDavid Howells /*
452248f219cSDavid Howells  * Detach a call from its owning socket.
4538c3e34a4SDavid Howells  */
4548d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
4558c3e34a4SDavid Howells {
456a84a46d7SDavid Howells 	const void *here = __builtin_return_address(0);
457248f219cSDavid Howells 	struct rxrpc_connection *conn = call->conn;
458248f219cSDavid Howells 	bool put = false;
459248f219cSDavid Howells 
460248f219cSDavid Howells 	_enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
461248f219cSDavid Howells 
46248c9e0ecSDavid Howells 	trace_rxrpc_call(call->debug_id, rxrpc_call_release,
46348c9e0ecSDavid Howells 			 atomic_read(&call->usage),
464a84a46d7SDavid Howells 			 here, (const void *)call->flags);
4658c3e34a4SDavid Howells 
466a84a46d7SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
467e34d4234SDavid Howells 
4688c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
4698c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
4708c3e34a4SDavid Howells 		BUG();
4718c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
4728c3e34a4SDavid Howells 
473248f219cSDavid Howells 	del_timer_sync(&call->timer);
4748c3e34a4SDavid Howells 
475248f219cSDavid Howells 	/* Make sure we don't get any more notifications */
476248f219cSDavid Howells 	write_lock_bh(&rx->recvmsg_lock);
477e653cfe4SDavid Howells 
478248f219cSDavid Howells 	if (!list_empty(&call->recvmsg_link)) {
4798c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
4808c3e34a4SDavid Howells 		       call, call->events, call->flags);
481248f219cSDavid Howells 		list_del(&call->recvmsg_link);
482248f219cSDavid Howells 		put = true;
483248f219cSDavid Howells 	}
484248f219cSDavid Howells 
485248f219cSDavid Howells 	/* list_empty() must return false in rxrpc_notify_socket() */
486248f219cSDavid Howells 	call->recvmsg_link.next = NULL;
487248f219cSDavid Howells 	call->recvmsg_link.prev = NULL;
488248f219cSDavid Howells 
489248f219cSDavid Howells 	write_unlock_bh(&rx->recvmsg_lock);
490248f219cSDavid Howells 	if (put)
491248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
492248f219cSDavid Howells 
493248f219cSDavid Howells 	write_lock(&rx->call_lock);
494248f219cSDavid Howells 
495248f219cSDavid Howells 	if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
4968c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
4978c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
4988d94aa38SDavid Howells 		rxrpc_put_call(call, rxrpc_call_put_userid);
4998c3e34a4SDavid Howells 	}
5008c3e34a4SDavid Howells 
501248f219cSDavid Howells 	list_del(&call->sock_link);
502248f219cSDavid Howells 	write_unlock(&rx->call_lock);
5038c3e34a4SDavid Howells 
504248f219cSDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
5058c3e34a4SDavid Howells 
5065273a191SDavid Howells 	if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
507e653cfe4SDavid Howells 		rxrpc_disconnect_call(call);
50891fcfbe8SDavid Howells 	if (call->security)
50991fcfbe8SDavid Howells 		call->security->free_call_crypto(call);
510e653cfe4SDavid Howells 
511a641fd00SDavid Howells 	rxrpc_cleanup_ring(call);
5128c3e34a4SDavid Howells 	_leave("");
5138c3e34a4SDavid Howells }
5148c3e34a4SDavid Howells 
5158c3e34a4SDavid Howells /*
5168c3e34a4SDavid Howells  * release all the calls associated with a socket
5178c3e34a4SDavid Howells  */
5188c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
5198c3e34a4SDavid Howells {
5208c3e34a4SDavid Howells 	struct rxrpc_call *call;
5218c3e34a4SDavid Howells 
5228c3e34a4SDavid Howells 	_enter("%p", rx);
5238c3e34a4SDavid Howells 
5240360da6dSDavid Howells 	while (!list_empty(&rx->to_be_accepted)) {
5250360da6dSDavid Howells 		call = list_entry(rx->to_be_accepted.next,
5260360da6dSDavid Howells 				  struct rxrpc_call, accept_link);
5270360da6dSDavid Howells 		list_del(&call->accept_link);
5283a92789aSDavid Howells 		rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
5290360da6dSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5300360da6dSDavid Howells 	}
5310360da6dSDavid Howells 
532248f219cSDavid Howells 	while (!list_empty(&rx->sock_calls)) {
533248f219cSDavid Howells 		call = list_entry(rx->sock_calls.next,
534248f219cSDavid Howells 				  struct rxrpc_call, sock_link);
535248f219cSDavid Howells 		rxrpc_get_call(call, rxrpc_call_got);
5363a92789aSDavid Howells 		rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
53726cb02aaSDavid Howells 		rxrpc_send_abort_packet(call);
5388d94aa38SDavid Howells 		rxrpc_release_call(rx, call);
539248f219cSDavid Howells 		rxrpc_put_call(call, rxrpc_call_put);
5408c3e34a4SDavid Howells 	}
5418c3e34a4SDavid Howells 
5428c3e34a4SDavid Howells 	_leave("");
5438c3e34a4SDavid Howells }
5448c3e34a4SDavid Howells 
5458c3e34a4SDavid Howells /*
5468c3e34a4SDavid Howells  * release a call
5478c3e34a4SDavid Howells  */
548fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
5498c3e34a4SDavid Howells {
550d3be4d24SDavid Howells 	struct rxrpc_net *rxnet = call->rxnet;
551e34d4234SDavid Howells 	const void *here = __builtin_return_address(0);
55248c9e0ecSDavid Howells 	unsigned int debug_id = call->debug_id;
5532ab27215SDavid Howells 	int n;
554e34d4234SDavid Howells 
5558c3e34a4SDavid Howells 	ASSERT(call != NULL);
5568c3e34a4SDavid Howells 
557e34d4234SDavid Howells 	n = atomic_dec_return(&call->usage);
55848c9e0ecSDavid Howells 	trace_rxrpc_call(debug_id, op, n, here, NULL);
559e34d4234SDavid Howells 	ASSERTCMP(n, >=, 0);
560e34d4234SDavid Howells 	if (n == 0) {
5618c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
562248f219cSDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
563e34d4234SDavid Howells 
5642baec2c3SDavid Howells 		if (!list_empty(&call->link)) {
5652baec2c3SDavid Howells 			write_lock(&rxnet->call_lock);
566248f219cSDavid Howells 			list_del_init(&call->link);
5672baec2c3SDavid Howells 			write_unlock(&rxnet->call_lock);
5682baec2c3SDavid Howells 		}
569e34d4234SDavid Howells 
5708d94aa38SDavid Howells 		rxrpc_cleanup_call(call);
571e34d4234SDavid Howells 	}
5728c3e34a4SDavid Howells }
5738c3e34a4SDavid Howells 
5748c3e34a4SDavid Howells /*
575963485d4SDavid Howells  * Final call destruction - but must be done in process context.
576dee46364SDavid Howells  */
577963485d4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work)
578dee46364SDavid Howells {
579963485d4SDavid Howells 	struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor);
580d3be4d24SDavid Howells 	struct rxrpc_net *rxnet = call->rxnet;
581dee46364SDavid Howells 
5825273a191SDavid Howells 	rxrpc_put_connection(call->conn);
583df5d8bf7SDavid Howells 	rxrpc_put_peer(call->peer);
584248f219cSDavid Howells 	kfree(call->rxtx_buffer);
585248f219cSDavid Howells 	kfree(call->rxtx_annotations);
586dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
587d3be4d24SDavid Howells 	if (atomic_dec_and_test(&rxnet->nr_calls))
5885bb053beSLinus Torvalds 		wake_up_var(&rxnet->nr_calls);
589dee46364SDavid Howells }
590dee46364SDavid Howells 
591dee46364SDavid Howells /*
592963485d4SDavid Howells  * Final call destruction under RCU.
593963485d4SDavid Howells  */
594963485d4SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
595963485d4SDavid Howells {
596963485d4SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
597963485d4SDavid Howells 
598963485d4SDavid Howells 	if (in_softirq()) {
599963485d4SDavid Howells 		INIT_WORK(&call->processor, rxrpc_destroy_call);
600963485d4SDavid Howells 		if (!rxrpc_queue_work(&call->processor))
601963485d4SDavid Howells 			BUG();
602963485d4SDavid Howells 	} else {
603963485d4SDavid Howells 		rxrpc_destroy_call(&call->processor);
604963485d4SDavid Howells 	}
605963485d4SDavid Howells }
606963485d4SDavid Howells 
607963485d4SDavid Howells /*
6088c3e34a4SDavid Howells  * clean up a call
6098c3e34a4SDavid Howells  */
61000e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call)
6118c3e34a4SDavid Howells {
612248f219cSDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
6138c3e34a4SDavid Howells 
6148c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
6158c3e34a4SDavid Howells 
616248f219cSDavid Howells 	del_timer_sync(&call->timer);
6178c3e34a4SDavid Howells 
6188d94aa38SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
6198c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
6208c3e34a4SDavid Howells 
621a641fd00SDavid Howells 	rxrpc_cleanup_ring(call);
622987db9f7SDavid Howells 	rxrpc_free_skb(call->tx_pending, rxrpc_skb_cleaned);
6238c3e34a4SDavid Howells 
624dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
6258c3e34a4SDavid Howells }
6268c3e34a4SDavid Howells 
6278c3e34a4SDavid Howells /*
6282baec2c3SDavid Howells  * Make sure that all calls are gone from a network namespace.  To reach this
6292baec2c3SDavid Howells  * point, any open UDP sockets in that namespace must have been closed, so any
6302baec2c3SDavid Howells  * outstanding calls cannot be doing I/O.
6318c3e34a4SDavid Howells  */
6322baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
6338c3e34a4SDavid Howells {
6348c3e34a4SDavid Howells 	struct rxrpc_call *call;
6358c3e34a4SDavid Howells 
6368c3e34a4SDavid Howells 	_enter("");
6378d94aa38SDavid Howells 
638b1302342SDavid Howells 	if (!list_empty(&rxnet->calls)) {
6392baec2c3SDavid Howells 		write_lock(&rxnet->call_lock);
6408c3e34a4SDavid Howells 
6412baec2c3SDavid Howells 		while (!list_empty(&rxnet->calls)) {
642b1302342SDavid Howells 			call = list_entry(rxnet->calls.next,
643b1302342SDavid Howells 					  struct rxrpc_call, link);
6448c3e34a4SDavid Howells 			_debug("Zapping call %p", call);
6458c3e34a4SDavid Howells 
646e34d4234SDavid Howells 			rxrpc_see_call(call);
6478c3e34a4SDavid Howells 			list_del_init(&call->link);
6488c3e34a4SDavid Howells 
649248f219cSDavid Howells 			pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
6508c3e34a4SDavid Howells 			       call, atomic_read(&call->usage),
6518c3e34a4SDavid Howells 			       rxrpc_call_states[call->state],
6528c3e34a4SDavid Howells 			       call->flags, call->events);
6538c3e34a4SDavid Howells 
6542baec2c3SDavid Howells 			write_unlock(&rxnet->call_lock);
6558c3e34a4SDavid Howells 			cond_resched();
6562baec2c3SDavid Howells 			write_lock(&rxnet->call_lock);
6578c3e34a4SDavid Howells 		}
6588c3e34a4SDavid Howells 
6592baec2c3SDavid Howells 		write_unlock(&rxnet->call_lock);
660b1302342SDavid Howells 	}
661d3be4d24SDavid Howells 
662d3be4d24SDavid Howells 	atomic_dec(&rxnet->nr_calls);
6635bb053beSLinus Torvalds 	wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
6648c3e34a4SDavid Howells }
665