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_RECV_REQUEST] = "SvRcvReq", 278c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", 288c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", 298c3e34a4SDavid Howells [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", 308c3e34a4SDavid Howells [RXRPC_CALL_COMPLETE] = "Complete", 31f5c17aaeSDavid Howells }; 32f5c17aaeSDavid Howells 33f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = { 34f5c17aaeSDavid Howells [RXRPC_CALL_SUCCEEDED] = "Complete", 358c3e34a4SDavid Howells [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", 368c3e34a4SDavid Howells [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", 37f5c17aaeSDavid Howells [RXRPC_CALL_LOCAL_ERROR] = "LocError", 388c3e34a4SDavid Howells [RXRPC_CALL_NETWORK_ERROR] = "NetError", 398c3e34a4SDavid Howells }; 408c3e34a4SDavid Howells 418c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar; 428c3e34a4SDavid Howells 43b7a7d674SDavid Howells static struct semaphore rxrpc_call_limiter = 44b7a7d674SDavid Howells __SEMAPHORE_INITIALIZER(rxrpc_call_limiter, 1000); 45b7a7d674SDavid Howells static struct semaphore rxrpc_kernel_call_limiter = 46b7a7d674SDavid Howells __SEMAPHORE_INITIALIZER(rxrpc_kernel_call_limiter, 1000); 47b7a7d674SDavid Howells 48e99e88a9SKees Cook static void rxrpc_call_timer_expired(struct timer_list *t) 49248f219cSDavid Howells { 50e99e88a9SKees Cook struct rxrpc_call *call = from_timer(call, t, timer); 51248f219cSDavid Howells 52248f219cSDavid Howells _enter("%d", call->debug_id); 53248f219cSDavid Howells 54a158bdd3SDavid Howells if (call->state < RXRPC_CALL_COMPLETE) { 55334dfbfcSDavid Howells trace_rxrpc_timer_expired(call, jiffies); 564a7f62f9SDavid Howells __rxrpc_queue_call(call); 574a7f62f9SDavid Howells } else { 584a7f62f9SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 59fc7ab6d2SDavid Howells } 60248f219cSDavid Howells } 618c3e34a4SDavid Howells 624a7f62f9SDavid Howells void rxrpc_reduce_call_timer(struct rxrpc_call *call, 634a7f62f9SDavid Howells unsigned long expire_at, 644a7f62f9SDavid Howells unsigned long now, 654a7f62f9SDavid Howells enum rxrpc_timer_trace why) 664a7f62f9SDavid Howells { 674a7f62f9SDavid Howells if (rxrpc_try_get_call(call, rxrpc_call_got_timer)) { 684a7f62f9SDavid Howells trace_rxrpc_timer(call, why, now); 694a7f62f9SDavid Howells if (timer_reduce(&call->timer, expire_at)) 704a7f62f9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_notimer); 714a7f62f9SDavid Howells } 724a7f62f9SDavid Howells } 734a7f62f9SDavid Howells 744a7f62f9SDavid Howells void rxrpc_delete_call_timer(struct rxrpc_call *call) 754a7f62f9SDavid Howells { 764a7f62f9SDavid Howells if (del_timer_sync(&call->timer)) 774a7f62f9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_timer); 784a7f62f9SDavid Howells } 794a7f62f9SDavid Howells 809faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 818c3e34a4SDavid Howells 828c3e34a4SDavid Howells /* 838c3e34a4SDavid Howells * find an extant server call 848c3e34a4SDavid Howells * - called in process context with IRQs enabled 858c3e34a4SDavid Howells */ 868c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 878c3e34a4SDavid Howells unsigned long user_call_ID) 888c3e34a4SDavid Howells { 898c3e34a4SDavid Howells struct rxrpc_call *call; 908c3e34a4SDavid Howells struct rb_node *p; 918c3e34a4SDavid Howells 928c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 938c3e34a4SDavid Howells 948c3e34a4SDavid Howells read_lock(&rx->call_lock); 958c3e34a4SDavid Howells 968c3e34a4SDavid Howells p = rx->calls.rb_node; 978c3e34a4SDavid Howells while (p) { 988c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 998c3e34a4SDavid Howells 1008c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 1018c3e34a4SDavid Howells p = p->rb_left; 1028c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 1038c3e34a4SDavid Howells p = p->rb_right; 1048c3e34a4SDavid Howells else 1058c3e34a4SDavid Howells goto found_extant_call; 1068c3e34a4SDavid Howells } 1078c3e34a4SDavid Howells 1088c3e34a4SDavid Howells read_unlock(&rx->call_lock); 1098c3e34a4SDavid Howells _leave(" = NULL"); 1108c3e34a4SDavid Howells return NULL; 1118c3e34a4SDavid Howells 1128c3e34a4SDavid Howells found_extant_call: 113fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 1148c3e34a4SDavid Howells read_unlock(&rx->call_lock); 115a0575429SDavid Howells _leave(" = %p [%d]", call, refcount_read(&call->ref)); 1168c3e34a4SDavid Howells return call; 1178c3e34a4SDavid Howells } 1188c3e34a4SDavid Howells 1198c3e34a4SDavid Howells /* 1208c3e34a4SDavid Howells * allocate a new call 1218c3e34a4SDavid Howells */ 122a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp, 123a25e21f0SDavid Howells unsigned int debug_id) 1248c3e34a4SDavid Howells { 1258c3e34a4SDavid Howells struct rxrpc_call *call; 126d3be4d24SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 1278c3e34a4SDavid Howells 1288c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1298c3e34a4SDavid Howells if (!call) 1308c3e34a4SDavid Howells return NULL; 1318c3e34a4SDavid Howells 132540b1c48SDavid Howells mutex_init(&call->user_mutex); 1339faaff59SDavid Howells 1349faaff59SDavid Howells /* Prevent lockdep reporting a deadlock false positive between the afs 1359faaff59SDavid Howells * filesystem and sys_sendmsg() via the mmap sem. 1369faaff59SDavid Howells */ 1379faaff59SDavid Howells if (rx->sk.sk_kern_sock) 1389faaff59SDavid Howells lockdep_set_class(&call->user_mutex, 1399faaff59SDavid Howells &rxrpc_call_user_mutex_lock_class_key); 1409faaff59SDavid Howells 141e99e88a9SKees Cook timer_setup(&call->timer, rxrpc_call_timer_expired, 0); 1428c3e34a4SDavid Howells INIT_WORK(&call->processor, &rxrpc_process_call); 143999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 14445025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1458c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 146248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 147248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 148a4ea4c47SDavid Howells INIT_LIST_HEAD(&call->tx_buffer); 1495d7edbc9SDavid Howells skb_queue_head_init(&call->recvmsg_queue); 1505d7edbc9SDavid Howells skb_queue_head_init(&call->rx_oos_queue); 15145025bceSDavid Howells init_waitqueue_head(&call->waitq); 15220acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 153a4ea4c47SDavid Howells spin_lock_init(&call->tx_lock); 154c1e15b49SDavid Howells spin_lock_init(&call->input_lock); 155d57a3a15SDavid Howells spin_lock_init(&call->acks_ack_lock); 1568c3e34a4SDavid Howells rwlock_init(&call->state_lock); 157a0575429SDavid Howells refcount_set(&call->ref, 1); 158a25e21f0SDavid Howells call->debug_id = debug_id; 159e754eba6SDavid Howells call->tx_total_len = -1; 160a158bdd3SDavid Howells call->next_rx_timo = 20 * HZ; 161a158bdd3SDavid Howells call->next_req_timo = 1 * HZ; 1625d7edbc9SDavid Howells atomic64_set(&call->ackr_window, 0x100000001ULL); 1638c3e34a4SDavid Howells 1648c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1658c3e34a4SDavid Howells 16675e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 167248f219cSDavid Howells call->tx_winsize = 16; 16857494343SDavid Howells 1691fc4fa2aSDavid Howells if (RXRPC_TX_SMSS > 2190) 17057494343SDavid Howells call->cong_cwnd = 2; 1711fc4fa2aSDavid Howells else if (RXRPC_TX_SMSS > 1095) 1721fc4fa2aSDavid Howells call->cong_cwnd = 3; 1731fc4fa2aSDavid Howells else 1741fc4fa2aSDavid Howells call->cong_cwnd = 4; 175a4ea4c47SDavid Howells call->cong_ssthresh = RXRPC_TX_MAX_WINDOW; 176d3be4d24SDavid Howells 177d3be4d24SDavid Howells call->rxnet = rxnet; 1784700c4d8SDavid Howells call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK; 179d3be4d24SDavid Howells atomic_inc(&rxnet->nr_calls); 1808c3e34a4SDavid Howells return call; 1818c3e34a4SDavid Howells } 1828c3e34a4SDavid Howells 1838c3e34a4SDavid Howells /* 184999b69f8SDavid Howells * Allocate a new client call. 1858c3e34a4SDavid Howells */ 1869faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 1879faaff59SDavid Howells struct sockaddr_rxrpc *srx, 188a25e21f0SDavid Howells gfp_t gfp, 189a25e21f0SDavid Howells unsigned int debug_id) 1908c3e34a4SDavid Howells { 1918c3e34a4SDavid Howells struct rxrpc_call *call; 19257494343SDavid Howells ktime_t now; 1938c3e34a4SDavid Howells 1948c3e34a4SDavid Howells _enter(""); 1958c3e34a4SDavid Howells 196a25e21f0SDavid Howells call = rxrpc_alloc_call(rx, gfp, debug_id); 1978c3e34a4SDavid Howells if (!call) 1988c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 199999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 200999b69f8SDavid Howells call->service_id = srx->srx_service; 20157494343SDavid Howells now = ktime_get_real(); 20257494343SDavid Howells call->acks_latest_ts = now; 20357494343SDavid Howells call->cong_tstamp = now; 204999b69f8SDavid Howells 205999b69f8SDavid Howells _leave(" = %p", call); 206999b69f8SDavid Howells return call; 207999b69f8SDavid Howells } 208999b69f8SDavid Howells 209999b69f8SDavid Howells /* 210248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 211999b69f8SDavid Howells */ 212248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 213999b69f8SDavid Howells { 214a158bdd3SDavid Howells unsigned long now = jiffies; 215a158bdd3SDavid Howells unsigned long j = now + MAX_JIFFY_OFFSET; 216999b69f8SDavid Howells 217530403d9SDavid Howells call->delay_ack_at = j; 218bd1fdf8cSDavid Howells call->ack_lost_at = j; 219a158bdd3SDavid Howells call->resend_at = j; 220a158bdd3SDavid Howells call->ping_at = j; 221a158bdd3SDavid Howells call->expect_rx_by = j; 222a158bdd3SDavid Howells call->expect_req_by = j; 223a158bdd3SDavid Howells call->expect_term_by = j; 224a158bdd3SDavid Howells call->timer.expires = now; 2258c3e34a4SDavid Howells } 2268c3e34a4SDavid Howells 2278c3e34a4SDavid Howells /* 228b7a7d674SDavid Howells * Wait for a call slot to become available. 229b7a7d674SDavid Howells */ 230b7a7d674SDavid Howells static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp) 231b7a7d674SDavid Howells { 232b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 233b7a7d674SDavid Howells 234b7a7d674SDavid Howells if (p->kernel) 235b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 236b7a7d674SDavid Howells if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) { 237b7a7d674SDavid Howells down(limiter); 238b7a7d674SDavid Howells return limiter; 239b7a7d674SDavid Howells } 240b7a7d674SDavid Howells return down_interruptible(limiter) < 0 ? NULL : limiter; 241b7a7d674SDavid Howells } 242b7a7d674SDavid Howells 243b7a7d674SDavid Howells /* 244b7a7d674SDavid Howells * Release a call slot. 245b7a7d674SDavid Howells */ 246b7a7d674SDavid Howells static void rxrpc_put_call_slot(struct rxrpc_call *call) 247b7a7d674SDavid Howells { 248b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 249b7a7d674SDavid Howells 250b7a7d674SDavid Howells if (test_bit(RXRPC_CALL_KERNEL, &call->flags)) 251b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 252b7a7d674SDavid Howells up(limiter); 253b7a7d674SDavid Howells } 254b7a7d674SDavid Howells 255b7a7d674SDavid Howells /* 256540b1c48SDavid Howells * Set up a call for the given parameters. 257540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 258540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2598c3e34a4SDavid Howells */ 2608c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 26119ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 262999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 26348124178SDavid Howells struct rxrpc_call_params *p, 264a25e21f0SDavid Howells gfp_t gfp, 265a25e21f0SDavid Howells unsigned int debug_id) 266540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 26788f2a825SDavid Howells __acquires(&call->user_mutex) 2688c3e34a4SDavid Howells { 2698c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 270d3be4d24SDavid Howells struct rxrpc_net *rxnet; 271b7a7d674SDavid Howells struct semaphore *limiter; 2728c3e34a4SDavid Howells struct rb_node *parent, **pp; 273e34d4234SDavid Howells const void *here = __builtin_return_address(0); 274999b69f8SDavid Howells int ret; 2758c3e34a4SDavid Howells 27648124178SDavid Howells _enter("%p,%lx", rx, p->user_call_ID); 2778c3e34a4SDavid Howells 278b7a7d674SDavid Howells limiter = rxrpc_get_call_slot(p, gfp); 279b0f571ecSDavid Howells if (!limiter) { 280b0f571ecSDavid Howells release_sock(&rx->sk); 281b7a7d674SDavid Howells return ERR_PTR(-ERESTARTSYS); 282b0f571ecSDavid Howells } 283b7a7d674SDavid Howells 284a25e21f0SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id); 2858c3e34a4SDavid Howells if (IS_ERR(call)) { 286540b1c48SDavid Howells release_sock(&rx->sk); 287b7a7d674SDavid Howells up(limiter); 2888c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 2898c3e34a4SDavid Howells return call; 2908c3e34a4SDavid Howells } 2918c3e34a4SDavid Howells 292e138aa7dSDavid Howells call->interruptibility = p->interruptibility; 29348124178SDavid Howells call->tx_total_len = p->tx_total_len; 29448c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_new_client, 295a0575429SDavid Howells refcount_read(&call->ref), 29648124178SDavid Howells here, (const void *)p->user_call_ID); 297b7a7d674SDavid Howells if (p->kernel) 298b7a7d674SDavid Howells __set_bit(RXRPC_CALL_KERNEL, &call->flags); 299e34d4234SDavid Howells 300540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 301540b1c48SDavid Howells * will be acting outside the socket lock. 302540b1c48SDavid Howells */ 303540b1c48SDavid Howells mutex_lock(&call->user_mutex); 304540b1c48SDavid Howells 305999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 3068c3e34a4SDavid Howells write_lock(&rx->call_lock); 3078c3e34a4SDavid Howells 3088c3e34a4SDavid Howells pp = &rx->calls.rb_node; 3098c3e34a4SDavid Howells parent = NULL; 3108c3e34a4SDavid Howells while (*pp) { 3118c3e34a4SDavid Howells parent = *pp; 3128c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 3138c3e34a4SDavid Howells 31448124178SDavid Howells if (p->user_call_ID < xcall->user_call_ID) 3158c3e34a4SDavid Howells pp = &(*pp)->rb_left; 31648124178SDavid Howells else if (p->user_call_ID > xcall->user_call_ID) 3178c3e34a4SDavid Howells pp = &(*pp)->rb_right; 3188c3e34a4SDavid Howells else 319357f5ef6SDavid Howells goto error_dup_user_ID; 3208c3e34a4SDavid Howells } 3218c3e34a4SDavid Howells 322248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 32348124178SDavid Howells call->user_call_ID = p->user_call_ID; 324357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 325fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got_userid); 3268c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 3278c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 328248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 329248f219cSDavid Howells 3308c3e34a4SDavid Howells write_unlock(&rx->call_lock); 3318c3e34a4SDavid Howells 332d3be4d24SDavid Howells rxnet = call->rxnet; 333ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 334ad25f5cbSDavid Howells list_add_tail_rcu(&call->link, &rxnet->calls); 335ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 3368c3e34a4SDavid Howells 337540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 338540b1c48SDavid Howells release_sock(&rx->sk); 339540b1c48SDavid Howells 340248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 341248f219cSDavid Howells * including channel number and call ID. 342248f219cSDavid Howells */ 3435e33a23bSDavid Howells ret = rxrpc_connect_call(rx, call, cp, srx, gfp); 344999b69f8SDavid Howells if (ret < 0) 34565550098SDavid Howells goto error_attached_to_socket; 346999b69f8SDavid Howells 34748c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_connected, 348a0575429SDavid Howells refcount_read(&call->ref), here, NULL); 349a84a46d7SDavid Howells 350248f219cSDavid Howells rxrpc_start_call_timer(call); 351248f219cSDavid Howells 3528c3e34a4SDavid Howells _leave(" = %p [new]", call); 3538c3e34a4SDavid Howells return call; 3548c3e34a4SDavid Howells 3558c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 3568c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 3578c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 3588c3e34a4SDavid Howells * same time in different threads. 3598c3e34a4SDavid Howells */ 360357f5ef6SDavid Howells error_dup_user_ID: 3618c3e34a4SDavid Howells write_unlock(&rx->call_lock); 362540b1c48SDavid Howells release_sock(&rx->sk); 363357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 36465550098SDavid Howells RX_CALL_DEAD, -EEXIST); 36548c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_error, 366a0575429SDavid Howells refcount_read(&call->ref), here, ERR_PTR(-EEXIST)); 367357f5ef6SDavid Howells rxrpc_release_call(rx, call); 368540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 369357f5ef6SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 37065550098SDavid Howells _leave(" = -EEXIST"); 37165550098SDavid Howells return ERR_PTR(-EEXIST); 37265550098SDavid Howells 37365550098SDavid Howells /* We got an error, but the call is attached to the socket and is in 37465550098SDavid Howells * need of release. However, we might now race with recvmsg() when 37565550098SDavid Howells * completing the call queues it. Return 0 from sys_sendmsg() and 37665550098SDavid Howells * leave the error to recvmsg() to deal with. 37765550098SDavid Howells */ 37865550098SDavid Howells error_attached_to_socket: 37965550098SDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_error, 380a0575429SDavid Howells refcount_read(&call->ref), here, ERR_PTR(ret)); 38165550098SDavid Howells set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); 38265550098SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 38365550098SDavid Howells RX_CALL_DEAD, ret); 38465550098SDavid Howells _leave(" = c=%08x [err]", call->debug_id); 38565550098SDavid Howells return call; 3868c3e34a4SDavid Howells } 3878c3e34a4SDavid Howells 3888c3e34a4SDavid Howells /* 389248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 390248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 3918c3e34a4SDavid Howells */ 392248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 393248f219cSDavid Howells struct rxrpc_call *call, 39442886ffeSDavid Howells struct sk_buff *skb) 3958c3e34a4SDavid Howells { 396248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 39742886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 398248f219cSDavid Howells u32 chan; 3998c3e34a4SDavid Howells 400248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 4018c3e34a4SDavid Howells 402248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 403248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 404248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 405248f219cSDavid Howells call->cid = sp->hdr.cid; 406248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 40757494343SDavid Howells call->cong_tstamp = skb->tstamp; 4088c3e34a4SDavid Howells 409248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 410248f219cSDavid Howells * only defending against the data_ready handler (which we're called 411248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 412248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 413248f219cSDavid Howells * call pointer). 4148c3e34a4SDavid Howells */ 415248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 416248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 417248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 418a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 4198c3e34a4SDavid Howells 420*2cc80086SDavid Howells spin_lock(&conn->peer->lock); 421*2cc80086SDavid Howells hlist_add_head_rcu(&call->error_link, &conn->peer->error_targets); 422*2cc80086SDavid Howells spin_unlock(&conn->peer->lock); 4238c3e34a4SDavid Howells 424248f219cSDavid Howells rxrpc_start_call_timer(call); 425248f219cSDavid Howells _leave(""); 4268c3e34a4SDavid Howells } 4278c3e34a4SDavid Howells 4288c3e34a4SDavid Howells /* 4298d94aa38SDavid Howells * Queue a call's work processor, getting a ref to pass to the work queue. 4308d94aa38SDavid Howells */ 4318d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call) 4328d94aa38SDavid Howells { 4338d94aa38SDavid Howells const void *here = __builtin_return_address(0); 434a0575429SDavid Howells int n; 435a0575429SDavid Howells 436a0575429SDavid Howells if (!__refcount_inc_not_zero(&call->ref, &n)) 4378d94aa38SDavid Howells return false; 4388d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 43948c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1, 44048c9e0ecSDavid Howells here, NULL); 4418d94aa38SDavid Howells else 4428d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4438d94aa38SDavid Howells return true; 4448d94aa38SDavid Howells } 4458d94aa38SDavid Howells 4468d94aa38SDavid Howells /* 4478d94aa38SDavid Howells * Queue a call's work processor, passing the callers ref to the work queue. 4488d94aa38SDavid Howells */ 4498d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call) 4508d94aa38SDavid Howells { 4518d94aa38SDavid Howells const void *here = __builtin_return_address(0); 452a0575429SDavid Howells int n = refcount_read(&call->ref); 4538d94aa38SDavid Howells ASSERTCMP(n, >=, 1); 4548d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 45548c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n, 45648c9e0ecSDavid Howells here, NULL); 4578d94aa38SDavid Howells else 4588d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4598d94aa38SDavid Howells return true; 4608d94aa38SDavid Howells } 4618d94aa38SDavid Howells 4628d94aa38SDavid Howells /* 463e34d4234SDavid Howells * Note the re-emergence of a call. 464e34d4234SDavid Howells */ 465e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call) 466e34d4234SDavid Howells { 467e34d4234SDavid Howells const void *here = __builtin_return_address(0); 468e34d4234SDavid Howells if (call) { 469a0575429SDavid Howells int n = refcount_read(&call->ref); 470e34d4234SDavid Howells 47148c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n, 47248c9e0ecSDavid Howells here, NULL); 473e34d4234SDavid Howells } 474e34d4234SDavid Howells } 475e34d4234SDavid Howells 4764a7f62f9SDavid Howells bool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 4774a7f62f9SDavid Howells { 4784a7f62f9SDavid Howells const void *here = __builtin_return_address(0); 479a0575429SDavid Howells int n; 4804a7f62f9SDavid Howells 481a0575429SDavid Howells if (!__refcount_inc_not_zero(&call->ref, &n)) 4824a7f62f9SDavid Howells return false; 483a0575429SDavid Howells trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL); 4844a7f62f9SDavid Howells return true; 4854a7f62f9SDavid Howells } 4864a7f62f9SDavid Howells 487e34d4234SDavid Howells /* 488e34d4234SDavid Howells * Note the addition of a ref on a call. 489e34d4234SDavid Howells */ 490fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 491e34d4234SDavid Howells { 492e34d4234SDavid Howells const void *here = __builtin_return_address(0); 493a0575429SDavid Howells int n; 494e34d4234SDavid Howells 495a0575429SDavid Howells __refcount_inc(&call->ref, &n); 496a0575429SDavid Howells trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL); 497e34d4234SDavid Howells } 498e34d4234SDavid Howells 499e34d4234SDavid Howells /* 500a4ea4c47SDavid Howells * Clean up the Rx skb ring. 501a641fd00SDavid Howells */ 502a641fd00SDavid Howells static void rxrpc_cleanup_ring(struct rxrpc_call *call) 503a641fd00SDavid Howells { 5045d7edbc9SDavid Howells skb_queue_purge(&call->recvmsg_queue); 5055d7edbc9SDavid Howells skb_queue_purge(&call->rx_oos_queue); 506a641fd00SDavid Howells } 507a641fd00SDavid Howells 508a641fd00SDavid Howells /* 509248f219cSDavid Howells * Detach a call from its owning socket. 5108c3e34a4SDavid Howells */ 5118d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 5128c3e34a4SDavid Howells { 513a84a46d7SDavid Howells const void *here = __builtin_return_address(0); 514248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 515248f219cSDavid Howells bool put = false; 516248f219cSDavid Howells 517a0575429SDavid Howells _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref)); 518248f219cSDavid Howells 51948c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_release, 520a0575429SDavid Howells refcount_read(&call->ref), 521a84a46d7SDavid Howells here, (const void *)call->flags); 5228c3e34a4SDavid Howells 523a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 524e34d4234SDavid Howells 5258c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 5268c3e34a4SDavid Howells BUG(); 5278c3e34a4SDavid Howells 528b7a7d674SDavid Howells rxrpc_put_call_slot(call); 5294a7f62f9SDavid Howells rxrpc_delete_call_timer(call); 5308c3e34a4SDavid Howells 531248f219cSDavid Howells /* Make sure we don't get any more notifications */ 532248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 533e653cfe4SDavid Howells 534248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 5358c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 5368c3e34a4SDavid Howells call, call->events, call->flags); 537248f219cSDavid Howells list_del(&call->recvmsg_link); 538248f219cSDavid Howells put = true; 539248f219cSDavid Howells } 540248f219cSDavid Howells 541248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 542248f219cSDavid Howells call->recvmsg_link.next = NULL; 543248f219cSDavid Howells call->recvmsg_link.prev = NULL; 544248f219cSDavid Howells 545248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 546248f219cSDavid Howells if (put) 547248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 548248f219cSDavid Howells 549248f219cSDavid Howells write_lock(&rx->call_lock); 550248f219cSDavid Howells 551248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 5528c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 5538c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 5548d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid); 5558c3e34a4SDavid Howells } 5568c3e34a4SDavid Howells 557248f219cSDavid Howells list_del(&call->sock_link); 558248f219cSDavid Howells write_unlock(&rx->call_lock); 5598c3e34a4SDavid Howells 560248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 5618c3e34a4SDavid Howells 5625273a191SDavid Howells if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 563e653cfe4SDavid Howells rxrpc_disconnect_call(call); 56491fcfbe8SDavid Howells if (call->security) 56591fcfbe8SDavid Howells call->security->free_call_crypto(call); 5668c3e34a4SDavid Howells _leave(""); 5678c3e34a4SDavid Howells } 5688c3e34a4SDavid Howells 5698c3e34a4SDavid Howells /* 5708c3e34a4SDavid Howells * release all the calls associated with a socket 5718c3e34a4SDavid Howells */ 5728c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 5738c3e34a4SDavid Howells { 5748c3e34a4SDavid Howells struct rxrpc_call *call; 5758c3e34a4SDavid Howells 5768c3e34a4SDavid Howells _enter("%p", rx); 5778c3e34a4SDavid Howells 5780360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 5790360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 5800360da6dSDavid Howells struct rxrpc_call, accept_link); 5810360da6dSDavid Howells list_del(&call->accept_link); 5823a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 5830360da6dSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5840360da6dSDavid Howells } 5850360da6dSDavid Howells 586248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 587248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 588248f219cSDavid Howells struct rxrpc_call, sock_link); 589248f219cSDavid Howells rxrpc_get_call(call, rxrpc_call_got); 5903a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 59126cb02aaSDavid Howells rxrpc_send_abort_packet(call); 5928d94aa38SDavid Howells rxrpc_release_call(rx, call); 593248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5948c3e34a4SDavid Howells } 5958c3e34a4SDavid Howells 5968c3e34a4SDavid Howells _leave(""); 5978c3e34a4SDavid Howells } 5988c3e34a4SDavid Howells 5998c3e34a4SDavid Howells /* 6008c3e34a4SDavid Howells * release a call 6018c3e34a4SDavid Howells */ 602fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 6038c3e34a4SDavid Howells { 604d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 605e34d4234SDavid Howells const void *here = __builtin_return_address(0); 60648c9e0ecSDavid Howells unsigned int debug_id = call->debug_id; 607a0575429SDavid Howells bool dead; 6082ab27215SDavid Howells int n; 609e34d4234SDavid Howells 6108c3e34a4SDavid Howells ASSERT(call != NULL); 6118c3e34a4SDavid Howells 612a0575429SDavid Howells dead = __refcount_dec_and_test(&call->ref, &n); 61348c9e0ecSDavid Howells trace_rxrpc_call(debug_id, op, n, here, NULL); 614a0575429SDavid Howells if (dead) { 6158c3e34a4SDavid Howells _debug("call %d dead", call->debug_id); 616248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 617e34d4234SDavid Howells 6182baec2c3SDavid Howells if (!list_empty(&call->link)) { 619ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 620248f219cSDavid Howells list_del_init(&call->link); 621ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 6222baec2c3SDavid Howells } 623e34d4234SDavid Howells 6248d94aa38SDavid Howells rxrpc_cleanup_call(call); 625e34d4234SDavid Howells } 6268c3e34a4SDavid Howells } 6278c3e34a4SDavid Howells 6288c3e34a4SDavid Howells /* 629963485d4SDavid Howells * Final call destruction - but must be done in process context. 630dee46364SDavid Howells */ 631963485d4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work) 632dee46364SDavid Howells { 633963485d4SDavid Howells struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor); 634d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 635dee46364SDavid Howells 6364a7f62f9SDavid Howells rxrpc_delete_call_timer(call); 6374a7f62f9SDavid Howells 6385273a191SDavid Howells rxrpc_put_connection(call->conn); 639df5d8bf7SDavid Howells rxrpc_put_peer(call->peer); 640dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 641d3be4d24SDavid Howells if (atomic_dec_and_test(&rxnet->nr_calls)) 6425bb053beSLinus Torvalds wake_up_var(&rxnet->nr_calls); 643dee46364SDavid Howells } 644dee46364SDavid Howells 645dee46364SDavid Howells /* 646963485d4SDavid Howells * Final call destruction under RCU. 647963485d4SDavid Howells */ 648963485d4SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu) 649963485d4SDavid Howells { 650963485d4SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 651963485d4SDavid Howells 652963485d4SDavid Howells if (in_softirq()) { 653963485d4SDavid Howells INIT_WORK(&call->processor, rxrpc_destroy_call); 654963485d4SDavid Howells if (!rxrpc_queue_work(&call->processor)) 655963485d4SDavid Howells BUG(); 656963485d4SDavid Howells } else { 657963485d4SDavid Howells rxrpc_destroy_call(&call->processor); 658963485d4SDavid Howells } 659963485d4SDavid Howells } 660963485d4SDavid Howells 661963485d4SDavid Howells /* 6628c3e34a4SDavid Howells * clean up a call 6638c3e34a4SDavid Howells */ 66400e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 6658c3e34a4SDavid Howells { 666a4ea4c47SDavid Howells struct rxrpc_txbuf *txb; 667a4ea4c47SDavid Howells 6688c3e34a4SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 6698c3e34a4SDavid Howells 6708d94aa38SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 6718c3e34a4SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 6728c3e34a4SDavid Howells 673a641fd00SDavid Howells rxrpc_cleanup_ring(call); 674a4ea4c47SDavid Howells while ((txb = list_first_entry_or_null(&call->tx_buffer, 675a4ea4c47SDavid Howells struct rxrpc_txbuf, call_link))) { 676a4ea4c47SDavid Howells list_del(&txb->call_link); 677a4ea4c47SDavid Howells rxrpc_put_txbuf(txb, rxrpc_txbuf_put_cleaned); 678a4ea4c47SDavid Howells } 679a4ea4c47SDavid Howells rxrpc_put_txbuf(call->tx_pending, rxrpc_txbuf_put_cleaned); 680d57a3a15SDavid Howells rxrpc_free_skb(call->acks_soft_tbl, rxrpc_skb_cleaned); 6818c3e34a4SDavid Howells 682dee46364SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_destroy_call); 6838c3e34a4SDavid Howells } 6848c3e34a4SDavid Howells 6858c3e34a4SDavid Howells /* 6862baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 6872baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 6882baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 6898c3e34a4SDavid Howells */ 6902baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 6918c3e34a4SDavid Howells { 6928c3e34a4SDavid Howells struct rxrpc_call *call; 6938c3e34a4SDavid Howells 6948c3e34a4SDavid Howells _enter(""); 6958d94aa38SDavid Howells 696b1302342SDavid Howells if (!list_empty(&rxnet->calls)) { 697ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 6988c3e34a4SDavid Howells 6992baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 700b1302342SDavid Howells call = list_entry(rxnet->calls.next, 701b1302342SDavid Howells struct rxrpc_call, link); 7028c3e34a4SDavid Howells _debug("Zapping call %p", call); 7038c3e34a4SDavid Howells 704e34d4234SDavid Howells rxrpc_see_call(call); 7058c3e34a4SDavid Howells list_del_init(&call->link); 7068c3e34a4SDavid Howells 707248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 708a0575429SDavid Howells call, refcount_read(&call->ref), 7098c3e34a4SDavid Howells rxrpc_call_states[call->state], 7108c3e34a4SDavid Howells call->flags, call->events); 7118c3e34a4SDavid Howells 712ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 7138c3e34a4SDavid Howells cond_resched(); 714ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 7158c3e34a4SDavid Howells } 7168c3e34a4SDavid Howells 717ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 718b1302342SDavid Howells } 719d3be4d24SDavid Howells 720d3be4d24SDavid Howells atomic_dec(&rxnet->nr_calls); 7215bb053beSLinus Torvalds wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls)); 7228c3e34a4SDavid Howells } 723