18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* RxRPC individual remote procedure call handling
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/circ_buf.h>
138c2ecf20Sopenharmony_ci#include <linux/spinlock_types.h>
148c2ecf20Sopenharmony_ci#include <net/sock.h>
158c2ecf20Sopenharmony_ci#include <net/af_rxrpc.h>
168c2ecf20Sopenharmony_ci#include "ar-internal.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciconst char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
198c2ecf20Sopenharmony_ci	[RXRPC_CALL_UNINITIALISED]		= "Uninit  ",
208c2ecf20Sopenharmony_ci	[RXRPC_CALL_CLIENT_AWAIT_CONN]		= "ClWtConn",
218c2ecf20Sopenharmony_ci	[RXRPC_CALL_CLIENT_SEND_REQUEST]	= "ClSndReq",
228c2ecf20Sopenharmony_ci	[RXRPC_CALL_CLIENT_AWAIT_REPLY]		= "ClAwtRpl",
238c2ecf20Sopenharmony_ci	[RXRPC_CALL_CLIENT_RECV_REPLY]		= "ClRcvRpl",
248c2ecf20Sopenharmony_ci	[RXRPC_CALL_SERVER_PREALLOC]		= "SvPrealc",
258c2ecf20Sopenharmony_ci	[RXRPC_CALL_SERVER_SECURING]		= "SvSecure",
268c2ecf20Sopenharmony_ci	[RXRPC_CALL_SERVER_RECV_REQUEST]	= "SvRcvReq",
278c2ecf20Sopenharmony_ci	[RXRPC_CALL_SERVER_ACK_REQUEST]		= "SvAckReq",
288c2ecf20Sopenharmony_ci	[RXRPC_CALL_SERVER_SEND_REPLY]		= "SvSndRpl",
298c2ecf20Sopenharmony_ci	[RXRPC_CALL_SERVER_AWAIT_ACK]		= "SvAwtACK",
308c2ecf20Sopenharmony_ci	[RXRPC_CALL_COMPLETE]			= "Complete",
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciconst char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
348c2ecf20Sopenharmony_ci	[RXRPC_CALL_SUCCEEDED]			= "Complete",
358c2ecf20Sopenharmony_ci	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
368c2ecf20Sopenharmony_ci	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
378c2ecf20Sopenharmony_ci	[RXRPC_CALL_LOCAL_ERROR]		= "LocError",
388c2ecf20Sopenharmony_ci	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct kmem_cache *rxrpc_call_jar;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic struct semaphore rxrpc_call_limiter =
448c2ecf20Sopenharmony_ci	__SEMAPHORE_INITIALIZER(rxrpc_call_limiter, 1000);
458c2ecf20Sopenharmony_cistatic struct semaphore rxrpc_kernel_call_limiter =
468c2ecf20Sopenharmony_ci	__SEMAPHORE_INITIALIZER(rxrpc_kernel_call_limiter, 1000);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void rxrpc_call_timer_expired(struct timer_list *t)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct rxrpc_call *call = from_timer(call, t, timer);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	_enter("%d", call->debug_id);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (call->state < RXRPC_CALL_COMPLETE) {
558c2ecf20Sopenharmony_ci		trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies);
568c2ecf20Sopenharmony_ci		__rxrpc_queue_call(call);
578c2ecf20Sopenharmony_ci	} else {
588c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put);
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_civoid rxrpc_reduce_call_timer(struct rxrpc_call *call,
638c2ecf20Sopenharmony_ci			     unsigned long expire_at,
648c2ecf20Sopenharmony_ci			     unsigned long now,
658c2ecf20Sopenharmony_ci			     enum rxrpc_timer_trace why)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	if (rxrpc_try_get_call(call, rxrpc_call_got_timer)) {
688c2ecf20Sopenharmony_ci		trace_rxrpc_timer(call, why, now);
698c2ecf20Sopenharmony_ci		if (timer_reduce(&call->timer, expire_at))
708c2ecf20Sopenharmony_ci			rxrpc_put_call(call, rxrpc_call_put_notimer);
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_civoid rxrpc_delete_call_timer(struct rxrpc_call *call)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	if (del_timer_sync(&call->timer))
778c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put_timer);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic struct lock_class_key rxrpc_call_user_mutex_lock_class_key;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * find an extant server call
848c2ecf20Sopenharmony_ci * - called in process context with IRQs enabled
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistruct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
878c2ecf20Sopenharmony_ci					      unsigned long user_call_ID)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct rxrpc_call *call;
908c2ecf20Sopenharmony_ci	struct rb_node *p;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	_enter("%p,%lx", rx, user_call_ID);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	read_lock(&rx->call_lock);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	p = rx->calls.rb_node;
978c2ecf20Sopenharmony_ci	while (p) {
988c2ecf20Sopenharmony_ci		call = rb_entry(p, struct rxrpc_call, sock_node);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		if (user_call_ID < call->user_call_ID)
1018c2ecf20Sopenharmony_ci			p = p->rb_left;
1028c2ecf20Sopenharmony_ci		else if (user_call_ID > call->user_call_ID)
1038c2ecf20Sopenharmony_ci			p = p->rb_right;
1048c2ecf20Sopenharmony_ci		else
1058c2ecf20Sopenharmony_ci			goto found_extant_call;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	read_unlock(&rx->call_lock);
1098c2ecf20Sopenharmony_ci	_leave(" = NULL");
1108c2ecf20Sopenharmony_ci	return NULL;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cifound_extant_call:
1138c2ecf20Sopenharmony_ci	rxrpc_get_call(call, rxrpc_call_got);
1148c2ecf20Sopenharmony_ci	read_unlock(&rx->call_lock);
1158c2ecf20Sopenharmony_ci	_leave(" = %p [%d]", call, refcount_read(&call->ref));
1168c2ecf20Sopenharmony_ci	return call;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * allocate a new call
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cistruct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp,
1238c2ecf20Sopenharmony_ci				    unsigned int debug_id)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct rxrpc_call *call;
1268c2ecf20Sopenharmony_ci	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
1298c2ecf20Sopenharmony_ci	if (!call)
1308c2ecf20Sopenharmony_ci		return NULL;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
1338c2ecf20Sopenharmony_ci				    sizeof(struct sk_buff *),
1348c2ecf20Sopenharmony_ci				    gfp);
1358c2ecf20Sopenharmony_ci	if (!call->rxtx_buffer)
1368c2ecf20Sopenharmony_ci		goto nomem;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
1398c2ecf20Sopenharmony_ci	if (!call->rxtx_annotations)
1408c2ecf20Sopenharmony_ci		goto nomem_2;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	mutex_init(&call->user_mutex);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Prevent lockdep reporting a deadlock false positive between the afs
1458c2ecf20Sopenharmony_ci	 * filesystem and sys_sendmsg() via the mmap sem.
1468c2ecf20Sopenharmony_ci	 */
1478c2ecf20Sopenharmony_ci	if (rx->sk.sk_kern_sock)
1488c2ecf20Sopenharmony_ci		lockdep_set_class(&call->user_mutex,
1498c2ecf20Sopenharmony_ci				  &rxrpc_call_user_mutex_lock_class_key);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	timer_setup(&call->timer, rxrpc_call_timer_expired, 0);
1528c2ecf20Sopenharmony_ci	INIT_WORK(&call->processor, &rxrpc_process_call);
1538c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&call->link);
1548c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&call->chan_wait_link);
1558c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&call->accept_link);
1568c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&call->recvmsg_link);
1578c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&call->sock_link);
1588c2ecf20Sopenharmony_ci	init_waitqueue_head(&call->waitq);
1598c2ecf20Sopenharmony_ci	spin_lock_init(&call->lock);
1608c2ecf20Sopenharmony_ci	spin_lock_init(&call->notify_lock);
1618c2ecf20Sopenharmony_ci	spin_lock_init(&call->input_lock);
1628c2ecf20Sopenharmony_ci	rwlock_init(&call->state_lock);
1638c2ecf20Sopenharmony_ci	refcount_set(&call->ref, 1);
1648c2ecf20Sopenharmony_ci	call->debug_id = debug_id;
1658c2ecf20Sopenharmony_ci	call->tx_total_len = -1;
1668c2ecf20Sopenharmony_ci	call->next_rx_timo = 20 * HZ;
1678c2ecf20Sopenharmony_ci	call->next_req_timo = 1 * HZ;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Leave space in the ring to handle a maxed-out jumbo packet */
1728c2ecf20Sopenharmony_ci	call->rx_winsize = rxrpc_rx_window_size;
1738c2ecf20Sopenharmony_ci	call->tx_winsize = 16;
1748c2ecf20Sopenharmony_ci	call->rx_expect_next = 1;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	call->cong_cwnd = 2;
1778c2ecf20Sopenharmony_ci	call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	call->rxnet = rxnet;
1808c2ecf20Sopenharmony_ci	call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK;
1818c2ecf20Sopenharmony_ci	atomic_inc(&rxnet->nr_calls);
1828c2ecf20Sopenharmony_ci	return call;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cinomem_2:
1858c2ecf20Sopenharmony_ci	kfree(call->rxtx_buffer);
1868c2ecf20Sopenharmony_cinomem:
1878c2ecf20Sopenharmony_ci	kmem_cache_free(rxrpc_call_jar, call);
1888c2ecf20Sopenharmony_ci	return NULL;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/*
1928c2ecf20Sopenharmony_ci * Allocate a new client call.
1938c2ecf20Sopenharmony_ci */
1948c2ecf20Sopenharmony_cistatic struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
1958c2ecf20Sopenharmony_ci						  struct sockaddr_rxrpc *srx,
1968c2ecf20Sopenharmony_ci						  gfp_t gfp,
1978c2ecf20Sopenharmony_ci						  unsigned int debug_id)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct rxrpc_call *call;
2008c2ecf20Sopenharmony_ci	ktime_t now;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	_enter("");
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	call = rxrpc_alloc_call(rx, gfp, debug_id);
2058c2ecf20Sopenharmony_ci	if (!call)
2068c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2078c2ecf20Sopenharmony_ci	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
2088c2ecf20Sopenharmony_ci	call->service_id = srx->srx_service;
2098c2ecf20Sopenharmony_ci	call->tx_phase = true;
2108c2ecf20Sopenharmony_ci	now = ktime_get_real();
2118c2ecf20Sopenharmony_ci	call->acks_latest_ts = now;
2128c2ecf20Sopenharmony_ci	call->cong_tstamp = now;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	_leave(" = %p", call);
2158c2ecf20Sopenharmony_ci	return call;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/*
2198c2ecf20Sopenharmony_ci * Initiate the call ack/resend/expiry timer.
2208c2ecf20Sopenharmony_ci */
2218c2ecf20Sopenharmony_cistatic void rxrpc_start_call_timer(struct rxrpc_call *call)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	unsigned long now = jiffies;
2248c2ecf20Sopenharmony_ci	unsigned long j = now + MAX_JIFFY_OFFSET;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	call->ack_at = j;
2278c2ecf20Sopenharmony_ci	call->ack_lost_at = j;
2288c2ecf20Sopenharmony_ci	call->resend_at = j;
2298c2ecf20Sopenharmony_ci	call->ping_at = j;
2308c2ecf20Sopenharmony_ci	call->expect_rx_by = j;
2318c2ecf20Sopenharmony_ci	call->expect_req_by = j;
2328c2ecf20Sopenharmony_ci	call->expect_term_by = j;
2338c2ecf20Sopenharmony_ci	call->timer.expires = now;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/*
2378c2ecf20Sopenharmony_ci * Wait for a call slot to become available.
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_cistatic struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	struct semaphore *limiter = &rxrpc_call_limiter;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	if (p->kernel)
2448c2ecf20Sopenharmony_ci		limiter = &rxrpc_kernel_call_limiter;
2458c2ecf20Sopenharmony_ci	if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) {
2468c2ecf20Sopenharmony_ci		down(limiter);
2478c2ecf20Sopenharmony_ci		return limiter;
2488c2ecf20Sopenharmony_ci	}
2498c2ecf20Sopenharmony_ci	return down_interruptible(limiter) < 0 ? NULL : limiter;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/*
2538c2ecf20Sopenharmony_ci * Release a call slot.
2548c2ecf20Sopenharmony_ci */
2558c2ecf20Sopenharmony_cistatic void rxrpc_put_call_slot(struct rxrpc_call *call)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct semaphore *limiter = &rxrpc_call_limiter;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	if (test_bit(RXRPC_CALL_KERNEL, &call->flags))
2608c2ecf20Sopenharmony_ci		limiter = &rxrpc_kernel_call_limiter;
2618c2ecf20Sopenharmony_ci	up(limiter);
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/*
2658c2ecf20Sopenharmony_ci * Set up a call for the given parameters.
2668c2ecf20Sopenharmony_ci * - Called with the socket lock held, which it must release.
2678c2ecf20Sopenharmony_ci * - If it returns a call, the call's lock will need releasing by the caller.
2688c2ecf20Sopenharmony_ci */
2698c2ecf20Sopenharmony_cistruct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
2708c2ecf20Sopenharmony_ci					 struct rxrpc_conn_parameters *cp,
2718c2ecf20Sopenharmony_ci					 struct sockaddr_rxrpc *srx,
2728c2ecf20Sopenharmony_ci					 struct rxrpc_call_params *p,
2738c2ecf20Sopenharmony_ci					 gfp_t gfp,
2748c2ecf20Sopenharmony_ci					 unsigned int debug_id)
2758c2ecf20Sopenharmony_ci	__releases(&rx->sk.sk_lock.slock)
2768c2ecf20Sopenharmony_ci	__acquires(&call->user_mutex)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct rxrpc_call *call, *xcall;
2798c2ecf20Sopenharmony_ci	struct rxrpc_net *rxnet;
2808c2ecf20Sopenharmony_ci	struct semaphore *limiter;
2818c2ecf20Sopenharmony_ci	struct rb_node *parent, **pp;
2828c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
2838c2ecf20Sopenharmony_ci	int ret;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	_enter("%p,%lx", rx, p->user_call_ID);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	limiter = rxrpc_get_call_slot(p, gfp);
2888c2ecf20Sopenharmony_ci	if (!limiter) {
2898c2ecf20Sopenharmony_ci		release_sock(&rx->sk);
2908c2ecf20Sopenharmony_ci		return ERR_PTR(-ERESTARTSYS);
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id);
2948c2ecf20Sopenharmony_ci	if (IS_ERR(call)) {
2958c2ecf20Sopenharmony_ci		release_sock(&rx->sk);
2968c2ecf20Sopenharmony_ci		up(limiter);
2978c2ecf20Sopenharmony_ci		_leave(" = %ld", PTR_ERR(call));
2988c2ecf20Sopenharmony_ci		return call;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	call->interruptibility = p->interruptibility;
3028c2ecf20Sopenharmony_ci	call->tx_total_len = p->tx_total_len;
3038c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, rxrpc_call_new_client,
3048c2ecf20Sopenharmony_ci			 refcount_read(&call->ref),
3058c2ecf20Sopenharmony_ci			 here, (const void *)p->user_call_ID);
3068c2ecf20Sopenharmony_ci	if (p->kernel)
3078c2ecf20Sopenharmony_ci		__set_bit(RXRPC_CALL_KERNEL, &call->flags);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/* We need to protect a partially set up call against the user as we
3108c2ecf20Sopenharmony_ci	 * will be acting outside the socket lock.
3118c2ecf20Sopenharmony_ci	 */
3128c2ecf20Sopenharmony_ci	mutex_lock(&call->user_mutex);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* Publish the call, even though it is incompletely set up as yet */
3158c2ecf20Sopenharmony_ci	write_lock(&rx->call_lock);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	pp = &rx->calls.rb_node;
3188c2ecf20Sopenharmony_ci	parent = NULL;
3198c2ecf20Sopenharmony_ci	while (*pp) {
3208c2ecf20Sopenharmony_ci		parent = *pp;
3218c2ecf20Sopenharmony_ci		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci		if (p->user_call_ID < xcall->user_call_ID)
3248c2ecf20Sopenharmony_ci			pp = &(*pp)->rb_left;
3258c2ecf20Sopenharmony_ci		else if (p->user_call_ID > xcall->user_call_ID)
3268c2ecf20Sopenharmony_ci			pp = &(*pp)->rb_right;
3278c2ecf20Sopenharmony_ci		else
3288c2ecf20Sopenharmony_ci			goto error_dup_user_ID;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	rcu_assign_pointer(call->socket, rx);
3328c2ecf20Sopenharmony_ci	call->user_call_ID = p->user_call_ID;
3338c2ecf20Sopenharmony_ci	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
3348c2ecf20Sopenharmony_ci	rxrpc_get_call(call, rxrpc_call_got_userid);
3358c2ecf20Sopenharmony_ci	rb_link_node(&call->sock_node, parent, pp);
3368c2ecf20Sopenharmony_ci	rb_insert_color(&call->sock_node, &rx->calls);
3378c2ecf20Sopenharmony_ci	list_add(&call->sock_link, &rx->sock_calls);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	write_unlock(&rx->call_lock);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	rxnet = call->rxnet;
3428c2ecf20Sopenharmony_ci	write_lock(&rxnet->call_lock);
3438c2ecf20Sopenharmony_ci	list_add_tail(&call->link, &rxnet->calls);
3448c2ecf20Sopenharmony_ci	write_unlock(&rxnet->call_lock);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* From this point on, the call is protected by its own lock. */
3478c2ecf20Sopenharmony_ci	release_sock(&rx->sk);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	/* Set up or get a connection record and set the protocol parameters,
3508c2ecf20Sopenharmony_ci	 * including channel number and call ID.
3518c2ecf20Sopenharmony_ci	 */
3528c2ecf20Sopenharmony_ci	ret = rxrpc_connect_call(rx, call, cp, srx, gfp);
3538c2ecf20Sopenharmony_ci	if (ret < 0)
3548c2ecf20Sopenharmony_ci		goto error_attached_to_socket;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, rxrpc_call_connected,
3578c2ecf20Sopenharmony_ci			 refcount_read(&call->ref), here, NULL);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	rxrpc_start_call_timer(call);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	_leave(" = %p [new]", call);
3648c2ecf20Sopenharmony_ci	return call;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* We unexpectedly found the user ID in the list after taking
3678c2ecf20Sopenharmony_ci	 * the call_lock.  This shouldn't happen unless the user races
3688c2ecf20Sopenharmony_ci	 * with itself and tries to add the same user ID twice at the
3698c2ecf20Sopenharmony_ci	 * same time in different threads.
3708c2ecf20Sopenharmony_ci	 */
3718c2ecf20Sopenharmony_cierror_dup_user_ID:
3728c2ecf20Sopenharmony_ci	write_unlock(&rx->call_lock);
3738c2ecf20Sopenharmony_ci	release_sock(&rx->sk);
3748c2ecf20Sopenharmony_ci	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
3758c2ecf20Sopenharmony_ci				    RX_CALL_DEAD, -EEXIST);
3768c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, rxrpc_call_error,
3778c2ecf20Sopenharmony_ci			 refcount_read(&call->ref), here, ERR_PTR(-EEXIST));
3788c2ecf20Sopenharmony_ci	rxrpc_release_call(rx, call);
3798c2ecf20Sopenharmony_ci	mutex_unlock(&call->user_mutex);
3808c2ecf20Sopenharmony_ci	rxrpc_put_call(call, rxrpc_call_put);
3818c2ecf20Sopenharmony_ci	_leave(" = -EEXIST");
3828c2ecf20Sopenharmony_ci	return ERR_PTR(-EEXIST);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* We got an error, but the call is attached to the socket and is in
3858c2ecf20Sopenharmony_ci	 * need of release.  However, we might now race with recvmsg() when
3868c2ecf20Sopenharmony_ci	 * completing the call queues it.  Return 0 from sys_sendmsg() and
3878c2ecf20Sopenharmony_ci	 * leave the error to recvmsg() to deal with.
3888c2ecf20Sopenharmony_ci	 */
3898c2ecf20Sopenharmony_cierror_attached_to_socket:
3908c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, rxrpc_call_error,
3918c2ecf20Sopenharmony_ci			 refcount_read(&call->ref), here, ERR_PTR(ret));
3928c2ecf20Sopenharmony_ci	set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
3938c2ecf20Sopenharmony_ci	__rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
3948c2ecf20Sopenharmony_ci				    RX_CALL_DEAD, ret);
3958c2ecf20Sopenharmony_ci	_leave(" = c=%08x [err]", call->debug_id);
3968c2ecf20Sopenharmony_ci	return call;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/*
4008c2ecf20Sopenharmony_ci * Set up an incoming call.  call->conn points to the connection.
4018c2ecf20Sopenharmony_ci * This is called in BH context and isn't allowed to fail.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_civoid rxrpc_incoming_call(struct rxrpc_sock *rx,
4048c2ecf20Sopenharmony_ci			 struct rxrpc_call *call,
4058c2ecf20Sopenharmony_ci			 struct sk_buff *skb)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct rxrpc_connection *conn = call->conn;
4088c2ecf20Sopenharmony_ci	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
4098c2ecf20Sopenharmony_ci	u32 chan;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	_enter(",%d", call->conn->debug_id);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	rcu_assign_pointer(call->socket, rx);
4148c2ecf20Sopenharmony_ci	call->call_id		= sp->hdr.callNumber;
4158c2ecf20Sopenharmony_ci	call->service_id	= sp->hdr.serviceId;
4168c2ecf20Sopenharmony_ci	call->cid		= sp->hdr.cid;
4178c2ecf20Sopenharmony_ci	call->state		= RXRPC_CALL_SERVER_SECURING;
4188c2ecf20Sopenharmony_ci	call->cong_tstamp	= skb->tstamp;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/* Set the channel for this call.  We don't get channel_lock as we're
4218c2ecf20Sopenharmony_ci	 * only defending against the data_ready handler (which we're called
4228c2ecf20Sopenharmony_ci	 * from) and the RESPONSE packet parser (which is only really
4238c2ecf20Sopenharmony_ci	 * interested in call_counter and can cope with a disagreement with the
4248c2ecf20Sopenharmony_ci	 * call pointer).
4258c2ecf20Sopenharmony_ci	 */
4268c2ecf20Sopenharmony_ci	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
4278c2ecf20Sopenharmony_ci	conn->channels[chan].call_counter = call->call_id;
4288c2ecf20Sopenharmony_ci	conn->channels[chan].call_id = call->call_id;
4298c2ecf20Sopenharmony_ci	rcu_assign_pointer(conn->channels[chan].call, call);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	spin_lock(&conn->params.peer->lock);
4328c2ecf20Sopenharmony_ci	hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets);
4338c2ecf20Sopenharmony_ci	spin_unlock(&conn->params.peer->lock);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	rxrpc_start_call_timer(call);
4388c2ecf20Sopenharmony_ci	_leave("");
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci/*
4428c2ecf20Sopenharmony_ci * Queue a call's work processor, getting a ref to pass to the work queue.
4438c2ecf20Sopenharmony_ci */
4448c2ecf20Sopenharmony_cibool rxrpc_queue_call(struct rxrpc_call *call)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
4478c2ecf20Sopenharmony_ci	int n;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (!__refcount_inc_not_zero(&call->ref, &n))
4508c2ecf20Sopenharmony_ci		return false;
4518c2ecf20Sopenharmony_ci	if (rxrpc_queue_work(&call->processor))
4528c2ecf20Sopenharmony_ci		trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1,
4538c2ecf20Sopenharmony_ci				 here, NULL);
4548c2ecf20Sopenharmony_ci	else
4558c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4568c2ecf20Sopenharmony_ci	return true;
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci/*
4608c2ecf20Sopenharmony_ci * Queue a call's work processor, passing the callers ref to the work queue.
4618c2ecf20Sopenharmony_ci */
4628c2ecf20Sopenharmony_cibool __rxrpc_queue_call(struct rxrpc_call *call)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
4658c2ecf20Sopenharmony_ci	int n = refcount_read(&call->ref);
4668c2ecf20Sopenharmony_ci	ASSERTCMP(n, >=, 1);
4678c2ecf20Sopenharmony_ci	if (rxrpc_queue_work(&call->processor))
4688c2ecf20Sopenharmony_ci		trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n,
4698c2ecf20Sopenharmony_ci				 here, NULL);
4708c2ecf20Sopenharmony_ci	else
4718c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put_noqueue);
4728c2ecf20Sopenharmony_ci	return true;
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci/*
4768c2ecf20Sopenharmony_ci * Note the re-emergence of a call.
4778c2ecf20Sopenharmony_ci */
4788c2ecf20Sopenharmony_civoid rxrpc_see_call(struct rxrpc_call *call)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
4818c2ecf20Sopenharmony_ci	if (call) {
4828c2ecf20Sopenharmony_ci		int n = refcount_read(&call->ref);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n,
4858c2ecf20Sopenharmony_ci				 here, NULL);
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cibool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
4928c2ecf20Sopenharmony_ci	int n;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	if (!__refcount_inc_not_zero(&call->ref, &n))
4958c2ecf20Sopenharmony_ci		return false;
4968c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL);
4978c2ecf20Sopenharmony_ci	return true;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci/*
5018c2ecf20Sopenharmony_ci * Note the addition of a ref on a call.
5028c2ecf20Sopenharmony_ci */
5038c2ecf20Sopenharmony_civoid rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
5068c2ecf20Sopenharmony_ci	int n;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	__refcount_inc(&call->ref, &n);
5098c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL);
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci/*
5138c2ecf20Sopenharmony_ci * Clean up the RxTx skb ring.
5148c2ecf20Sopenharmony_ci */
5158c2ecf20Sopenharmony_cistatic void rxrpc_cleanup_ring(struct rxrpc_call *call)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	int i;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
5208c2ecf20Sopenharmony_ci		rxrpc_free_skb(call->rxtx_buffer[i], rxrpc_skb_cleaned);
5218c2ecf20Sopenharmony_ci		call->rxtx_buffer[i] = NULL;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/*
5268c2ecf20Sopenharmony_ci * Detach a call from its owning socket.
5278c2ecf20Sopenharmony_ci */
5288c2ecf20Sopenharmony_civoid rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
5318c2ecf20Sopenharmony_ci	struct rxrpc_connection *conn = call->conn;
5328c2ecf20Sopenharmony_ci	bool put = false;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	_enter("{%d,%d}", call->debug_id, refcount_read(&call->ref));
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	trace_rxrpc_call(call->debug_id, rxrpc_call_release,
5378c2ecf20Sopenharmony_ci			 refcount_read(&call->ref),
5388c2ecf20Sopenharmony_ci			 here, (const void *)call->flags);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	spin_lock_bh(&call->lock);
5438c2ecf20Sopenharmony_ci	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
5448c2ecf20Sopenharmony_ci		BUG();
5458c2ecf20Sopenharmony_ci	spin_unlock_bh(&call->lock);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	rxrpc_put_call_slot(call);
5488c2ecf20Sopenharmony_ci	rxrpc_delete_call_timer(call);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	/* Make sure we don't get any more notifications */
5518c2ecf20Sopenharmony_ci	write_lock_bh(&rx->recvmsg_lock);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	if (!list_empty(&call->recvmsg_link)) {
5548c2ecf20Sopenharmony_ci		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
5558c2ecf20Sopenharmony_ci		       call, call->events, call->flags);
5568c2ecf20Sopenharmony_ci		list_del(&call->recvmsg_link);
5578c2ecf20Sopenharmony_ci		put = true;
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/* list_empty() must return false in rxrpc_notify_socket() */
5618c2ecf20Sopenharmony_ci	call->recvmsg_link.next = NULL;
5628c2ecf20Sopenharmony_ci	call->recvmsg_link.prev = NULL;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	write_unlock_bh(&rx->recvmsg_lock);
5658c2ecf20Sopenharmony_ci	if (put)
5668c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	write_lock(&rx->call_lock);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
5718c2ecf20Sopenharmony_ci		rb_erase(&call->sock_node, &rx->calls);
5728c2ecf20Sopenharmony_ci		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
5738c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put_userid);
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	list_del(&call->sock_link);
5778c2ecf20Sopenharmony_ci	write_unlock(&rx->call_lock);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
5828c2ecf20Sopenharmony_ci		rxrpc_disconnect_call(call);
5838c2ecf20Sopenharmony_ci	if (call->security)
5848c2ecf20Sopenharmony_ci		call->security->free_call_crypto(call);
5858c2ecf20Sopenharmony_ci	_leave("");
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci/*
5898c2ecf20Sopenharmony_ci * release all the calls associated with a socket
5908c2ecf20Sopenharmony_ci */
5918c2ecf20Sopenharmony_civoid rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
5928c2ecf20Sopenharmony_ci{
5938c2ecf20Sopenharmony_ci	struct rxrpc_call *call;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	_enter("%p", rx);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	while (!list_empty(&rx->to_be_accepted)) {
5988c2ecf20Sopenharmony_ci		call = list_entry(rx->to_be_accepted.next,
5998c2ecf20Sopenharmony_ci				  struct rxrpc_call, accept_link);
6008c2ecf20Sopenharmony_ci		list_del(&call->accept_link);
6018c2ecf20Sopenharmony_ci		rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
6028c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put);
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	while (!list_empty(&rx->sock_calls)) {
6068c2ecf20Sopenharmony_ci		call = list_entry(rx->sock_calls.next,
6078c2ecf20Sopenharmony_ci				  struct rxrpc_call, sock_link);
6088c2ecf20Sopenharmony_ci		rxrpc_get_call(call, rxrpc_call_got);
6098c2ecf20Sopenharmony_ci		rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
6108c2ecf20Sopenharmony_ci		rxrpc_send_abort_packet(call);
6118c2ecf20Sopenharmony_ci		rxrpc_release_call(rx, call);
6128c2ecf20Sopenharmony_ci		rxrpc_put_call(call, rxrpc_call_put);
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	_leave("");
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci/*
6198c2ecf20Sopenharmony_ci * release a call
6208c2ecf20Sopenharmony_ci */
6218c2ecf20Sopenharmony_civoid rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct rxrpc_net *rxnet = call->rxnet;
6248c2ecf20Sopenharmony_ci	const void *here = __builtin_return_address(0);
6258c2ecf20Sopenharmony_ci	unsigned int debug_id = call->debug_id;
6268c2ecf20Sopenharmony_ci	bool dead;
6278c2ecf20Sopenharmony_ci	int n;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	ASSERT(call != NULL);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	dead = __refcount_dec_and_test(&call->ref, &n);
6328c2ecf20Sopenharmony_ci	trace_rxrpc_call(debug_id, op, n, here, NULL);
6338c2ecf20Sopenharmony_ci	if (dead) {
6348c2ecf20Sopenharmony_ci		_debug("call %d dead", call->debug_id);
6358c2ecf20Sopenharmony_ci		ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci		if (!list_empty(&call->link)) {
6388c2ecf20Sopenharmony_ci			write_lock(&rxnet->call_lock);
6398c2ecf20Sopenharmony_ci			list_del_init(&call->link);
6408c2ecf20Sopenharmony_ci			write_unlock(&rxnet->call_lock);
6418c2ecf20Sopenharmony_ci		}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci		rxrpc_cleanup_call(call);
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci/*
6488c2ecf20Sopenharmony_ci * Final call destruction - but must be done in process context.
6498c2ecf20Sopenharmony_ci */
6508c2ecf20Sopenharmony_cistatic void rxrpc_destroy_call(struct work_struct *work)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor);
6538c2ecf20Sopenharmony_ci	struct rxrpc_net *rxnet = call->rxnet;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	rxrpc_delete_call_timer(call);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	rxrpc_put_connection(call->conn);
6588c2ecf20Sopenharmony_ci	rxrpc_put_peer(call->peer);
6598c2ecf20Sopenharmony_ci	kfree(call->rxtx_buffer);
6608c2ecf20Sopenharmony_ci	kfree(call->rxtx_annotations);
6618c2ecf20Sopenharmony_ci	kmem_cache_free(rxrpc_call_jar, call);
6628c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&rxnet->nr_calls))
6638c2ecf20Sopenharmony_ci		wake_up_var(&rxnet->nr_calls);
6648c2ecf20Sopenharmony_ci}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci/*
6678c2ecf20Sopenharmony_ci * Final call destruction under RCU.
6688c2ecf20Sopenharmony_ci */
6698c2ecf20Sopenharmony_cistatic void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (in_softirq()) {
6748c2ecf20Sopenharmony_ci		INIT_WORK(&call->processor, rxrpc_destroy_call);
6758c2ecf20Sopenharmony_ci		if (!rxrpc_queue_work(&call->processor))
6768c2ecf20Sopenharmony_ci			BUG();
6778c2ecf20Sopenharmony_ci	} else {
6788c2ecf20Sopenharmony_ci		rxrpc_destroy_call(&call->processor);
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci/*
6838c2ecf20Sopenharmony_ci * clean up a call
6848c2ecf20Sopenharmony_ci */
6858c2ecf20Sopenharmony_civoid rxrpc_cleanup_call(struct rxrpc_call *call)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	_net("DESTROY CALL %d", call->debug_id);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
6928c2ecf20Sopenharmony_ci	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	rxrpc_cleanup_ring(call);
6958c2ecf20Sopenharmony_ci	rxrpc_free_skb(call->tx_pending, rxrpc_skb_cleaned);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
6988c2ecf20Sopenharmony_ci}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci/*
7018c2ecf20Sopenharmony_ci * Make sure that all calls are gone from a network namespace.  To reach this
7028c2ecf20Sopenharmony_ci * point, any open UDP sockets in that namespace must have been closed, so any
7038c2ecf20Sopenharmony_ci * outstanding calls cannot be doing I/O.
7048c2ecf20Sopenharmony_ci */
7058c2ecf20Sopenharmony_civoid rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	struct rxrpc_call *call;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	_enter("");
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	if (!list_empty(&rxnet->calls)) {
7128c2ecf20Sopenharmony_ci		write_lock(&rxnet->call_lock);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci		while (!list_empty(&rxnet->calls)) {
7158c2ecf20Sopenharmony_ci			call = list_entry(rxnet->calls.next,
7168c2ecf20Sopenharmony_ci					  struct rxrpc_call, link);
7178c2ecf20Sopenharmony_ci			_debug("Zapping call %p", call);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci			rxrpc_see_call(call);
7208c2ecf20Sopenharmony_ci			list_del_init(&call->link);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci			pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
7238c2ecf20Sopenharmony_ci			       call, refcount_read(&call->ref),
7248c2ecf20Sopenharmony_ci			       rxrpc_call_states[call->state],
7258c2ecf20Sopenharmony_ci			       call->flags, call->events);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci			write_unlock(&rxnet->call_lock);
7288c2ecf20Sopenharmony_ci			cond_resched();
7298c2ecf20Sopenharmony_ci			write_lock(&rxnet->call_lock);
7308c2ecf20Sopenharmony_ci		}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci		write_unlock(&rxnet->call_lock);
7338c2ecf20Sopenharmony_ci	}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	atomic_dec(&rxnet->nr_calls);
7368c2ecf20Sopenharmony_ci	wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
7378c2ecf20Sopenharmony_ci}
738