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 48*15f661dcSDavid Howells void rxrpc_poke_call(struct rxrpc_call *call, enum rxrpc_call_poke_trace what) 49*15f661dcSDavid Howells { 50*15f661dcSDavid Howells struct rxrpc_local *local; 51*15f661dcSDavid Howells struct rxrpc_peer *peer = call->peer; 52*15f661dcSDavid Howells bool busy; 53*15f661dcSDavid Howells 54*15f661dcSDavid Howells if (WARN_ON_ONCE(!peer)) 55*15f661dcSDavid Howells return; 56*15f661dcSDavid Howells local = peer->local; 57*15f661dcSDavid Howells 58*15f661dcSDavid Howells if (call->state < RXRPC_CALL_COMPLETE) { 59*15f661dcSDavid Howells spin_lock_bh(&local->lock); 60*15f661dcSDavid Howells busy = !list_empty(&call->attend_link); 61*15f661dcSDavid Howells trace_rxrpc_poke_call(call, busy, what); 62*15f661dcSDavid Howells if (!busy) { 63*15f661dcSDavid Howells rxrpc_get_call(call, rxrpc_call_get_poke); 64*15f661dcSDavid Howells list_add_tail(&call->attend_link, &local->call_attend_q); 65*15f661dcSDavid Howells } 66*15f661dcSDavid Howells spin_unlock_bh(&local->lock); 67*15f661dcSDavid Howells rxrpc_wake_up_io_thread(local); 68*15f661dcSDavid Howells } 69*15f661dcSDavid Howells } 70*15f661dcSDavid Howells 71e99e88a9SKees Cook static void rxrpc_call_timer_expired(struct timer_list *t) 72248f219cSDavid Howells { 73e99e88a9SKees Cook struct rxrpc_call *call = from_timer(call, t, timer); 74248f219cSDavid Howells 75248f219cSDavid Howells _enter("%d", call->debug_id); 76248f219cSDavid Howells 77a158bdd3SDavid Howells if (call->state < RXRPC_CALL_COMPLETE) { 78334dfbfcSDavid Howells trace_rxrpc_timer_expired(call, jiffies); 793feda9d6SDavid Howells rxrpc_queue_call(call, rxrpc_call_queue_timer); 80fc7ab6d2SDavid Howells } 81248f219cSDavid Howells } 828c3e34a4SDavid Howells 834a7f62f9SDavid Howells void rxrpc_reduce_call_timer(struct rxrpc_call *call, 844a7f62f9SDavid Howells unsigned long expire_at, 854a7f62f9SDavid Howells unsigned long now, 864a7f62f9SDavid Howells enum rxrpc_timer_trace why) 874a7f62f9SDavid Howells { 884a7f62f9SDavid Howells trace_rxrpc_timer(call, why, now); 893feda9d6SDavid Howells timer_reduce(&call->timer, expire_at); 904a7f62f9SDavid Howells } 914a7f62f9SDavid Howells 929faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 938c3e34a4SDavid Howells 943feda9d6SDavid Howells static void rxrpc_destroy_call(struct work_struct *); 953feda9d6SDavid Howells 968c3e34a4SDavid Howells /* 978c3e34a4SDavid Howells * find an extant server call 988c3e34a4SDavid Howells * - called in process context with IRQs enabled 998c3e34a4SDavid Howells */ 1008c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 1018c3e34a4SDavid Howells unsigned long user_call_ID) 1028c3e34a4SDavid Howells { 1038c3e34a4SDavid Howells struct rxrpc_call *call; 1048c3e34a4SDavid Howells struct rb_node *p; 1058c3e34a4SDavid Howells 1068c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 1078c3e34a4SDavid Howells 1088c3e34a4SDavid Howells read_lock(&rx->call_lock); 1098c3e34a4SDavid Howells 1108c3e34a4SDavid Howells p = rx->calls.rb_node; 1118c3e34a4SDavid Howells while (p) { 1128c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 1138c3e34a4SDavid Howells 1148c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 1158c3e34a4SDavid Howells p = p->rb_left; 1168c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 1178c3e34a4SDavid Howells p = p->rb_right; 1188c3e34a4SDavid Howells else 1198c3e34a4SDavid Howells goto found_extant_call; 1208c3e34a4SDavid Howells } 1218c3e34a4SDavid Howells 1228c3e34a4SDavid Howells read_unlock(&rx->call_lock); 1238c3e34a4SDavid Howells _leave(" = NULL"); 1248c3e34a4SDavid Howells return NULL; 1258c3e34a4SDavid Howells 1268c3e34a4SDavid Howells found_extant_call: 127cb0fc0c9SDavid Howells rxrpc_get_call(call, rxrpc_call_get_sendmsg); 1288c3e34a4SDavid Howells read_unlock(&rx->call_lock); 129a0575429SDavid Howells _leave(" = %p [%d]", call, refcount_read(&call->ref)); 1308c3e34a4SDavid Howells return call; 1318c3e34a4SDavid Howells } 1328c3e34a4SDavid Howells 1338c3e34a4SDavid Howells /* 1348c3e34a4SDavid Howells * allocate a new call 1358c3e34a4SDavid Howells */ 136a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp, 137a25e21f0SDavid Howells unsigned int debug_id) 1388c3e34a4SDavid Howells { 1398c3e34a4SDavid Howells struct rxrpc_call *call; 140d3be4d24SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 1418c3e34a4SDavid Howells 1428c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1438c3e34a4SDavid Howells if (!call) 1448c3e34a4SDavid Howells return NULL; 1458c3e34a4SDavid Howells 146540b1c48SDavid Howells mutex_init(&call->user_mutex); 1479faaff59SDavid Howells 1489faaff59SDavid Howells /* Prevent lockdep reporting a deadlock false positive between the afs 1499faaff59SDavid Howells * filesystem and sys_sendmsg() via the mmap sem. 1509faaff59SDavid Howells */ 1519faaff59SDavid Howells if (rx->sk.sk_kern_sock) 1529faaff59SDavid Howells lockdep_set_class(&call->user_mutex, 1539faaff59SDavid Howells &rxrpc_call_user_mutex_lock_class_key); 1549faaff59SDavid Howells 155e99e88a9SKees Cook timer_setup(&call->timer, rxrpc_call_timer_expired, 0); 1563feda9d6SDavid Howells INIT_WORK(&call->processor, rxrpc_process_call); 1573feda9d6SDavid Howells INIT_WORK(&call->destroyer, rxrpc_destroy_call); 158999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 15945025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1608c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 161248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 162248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 163*15f661dcSDavid Howells INIT_LIST_HEAD(&call->attend_link); 164a4ea4c47SDavid Howells INIT_LIST_HEAD(&call->tx_buffer); 1655d7edbc9SDavid Howells skb_queue_head_init(&call->recvmsg_queue); 1665d7edbc9SDavid Howells skb_queue_head_init(&call->rx_oos_queue); 16745025bceSDavid Howells init_waitqueue_head(&call->waitq); 16820acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 169a4ea4c47SDavid Howells spin_lock_init(&call->tx_lock); 170d57a3a15SDavid Howells spin_lock_init(&call->acks_ack_lock); 1718c3e34a4SDavid Howells rwlock_init(&call->state_lock); 172a0575429SDavid Howells refcount_set(&call->ref, 1); 173a25e21f0SDavid Howells call->debug_id = debug_id; 174e754eba6SDavid Howells call->tx_total_len = -1; 175a158bdd3SDavid Howells call->next_rx_timo = 20 * HZ; 176a158bdd3SDavid Howells call->next_req_timo = 1 * HZ; 1775d7edbc9SDavid Howells atomic64_set(&call->ackr_window, 0x100000001ULL); 1788c3e34a4SDavid Howells 1798c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1808c3e34a4SDavid Howells 18175e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 182248f219cSDavid Howells call->tx_winsize = 16; 18357494343SDavid Howells 1841fc4fa2aSDavid Howells if (RXRPC_TX_SMSS > 2190) 18557494343SDavid Howells call->cong_cwnd = 2; 1861fc4fa2aSDavid Howells else if (RXRPC_TX_SMSS > 1095) 1871fc4fa2aSDavid Howells call->cong_cwnd = 3; 1881fc4fa2aSDavid Howells else 1891fc4fa2aSDavid Howells call->cong_cwnd = 4; 190a4ea4c47SDavid Howells call->cong_ssthresh = RXRPC_TX_MAX_WINDOW; 191d3be4d24SDavid Howells 192d3be4d24SDavid Howells call->rxnet = rxnet; 1934700c4d8SDavid Howells call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK; 194d3be4d24SDavid Howells atomic_inc(&rxnet->nr_calls); 1958c3e34a4SDavid Howells return call; 1968c3e34a4SDavid Howells } 1978c3e34a4SDavid Howells 1988c3e34a4SDavid Howells /* 199999b69f8SDavid Howells * Allocate a new client call. 2008c3e34a4SDavid Howells */ 2019faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 2029faaff59SDavid Howells struct sockaddr_rxrpc *srx, 203a25e21f0SDavid Howells gfp_t gfp, 204a25e21f0SDavid Howells unsigned int debug_id) 2058c3e34a4SDavid Howells { 2068c3e34a4SDavid Howells struct rxrpc_call *call; 20757494343SDavid Howells ktime_t now; 2088c3e34a4SDavid Howells 2098c3e34a4SDavid Howells _enter(""); 2108c3e34a4SDavid Howells 211a25e21f0SDavid Howells call = rxrpc_alloc_call(rx, gfp, debug_id); 2128c3e34a4SDavid Howells if (!call) 2138c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 214999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 215999b69f8SDavid Howells call->service_id = srx->srx_service; 21657494343SDavid Howells now = ktime_get_real(); 21757494343SDavid Howells call->acks_latest_ts = now; 21857494343SDavid Howells call->cong_tstamp = now; 219999b69f8SDavid Howells 220999b69f8SDavid Howells _leave(" = %p", call); 221999b69f8SDavid Howells return call; 222999b69f8SDavid Howells } 223999b69f8SDavid Howells 224999b69f8SDavid Howells /* 225248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 226999b69f8SDavid Howells */ 227248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 228999b69f8SDavid Howells { 229a158bdd3SDavid Howells unsigned long now = jiffies; 230a158bdd3SDavid Howells unsigned long j = now + MAX_JIFFY_OFFSET; 231999b69f8SDavid Howells 232530403d9SDavid Howells call->delay_ack_at = j; 233bd1fdf8cSDavid Howells call->ack_lost_at = j; 234a158bdd3SDavid Howells call->resend_at = j; 235a158bdd3SDavid Howells call->ping_at = j; 236a158bdd3SDavid Howells call->expect_rx_by = j; 237a158bdd3SDavid Howells call->expect_req_by = j; 238a158bdd3SDavid Howells call->expect_term_by = j; 239a158bdd3SDavid Howells call->timer.expires = now; 2408c3e34a4SDavid Howells } 2418c3e34a4SDavid Howells 2428c3e34a4SDavid Howells /* 243b7a7d674SDavid Howells * Wait for a call slot to become available. 244b7a7d674SDavid Howells */ 245b7a7d674SDavid Howells static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp) 246b7a7d674SDavid Howells { 247b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 248b7a7d674SDavid Howells 249b7a7d674SDavid Howells if (p->kernel) 250b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 251b7a7d674SDavid Howells if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) { 252b7a7d674SDavid Howells down(limiter); 253b7a7d674SDavid Howells return limiter; 254b7a7d674SDavid Howells } 255b7a7d674SDavid Howells return down_interruptible(limiter) < 0 ? NULL : limiter; 256b7a7d674SDavid Howells } 257b7a7d674SDavid Howells 258b7a7d674SDavid Howells /* 259b7a7d674SDavid Howells * Release a call slot. 260b7a7d674SDavid Howells */ 261b7a7d674SDavid Howells static void rxrpc_put_call_slot(struct rxrpc_call *call) 262b7a7d674SDavid Howells { 263b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 264b7a7d674SDavid Howells 265b7a7d674SDavid Howells if (test_bit(RXRPC_CALL_KERNEL, &call->flags)) 266b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 267b7a7d674SDavid Howells up(limiter); 268b7a7d674SDavid Howells } 269b7a7d674SDavid Howells 270b7a7d674SDavid Howells /* 271540b1c48SDavid Howells * Set up a call for the given parameters. 272540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 273540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2748c3e34a4SDavid Howells */ 2758c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 27619ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 277999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 27848124178SDavid Howells struct rxrpc_call_params *p, 279a25e21f0SDavid Howells gfp_t gfp, 280a25e21f0SDavid Howells unsigned int debug_id) 281540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 28288f2a825SDavid Howells __acquires(&call->user_mutex) 2838c3e34a4SDavid Howells { 2848c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 285d3be4d24SDavid Howells struct rxrpc_net *rxnet; 286b7a7d674SDavid Howells struct semaphore *limiter; 2878c3e34a4SDavid Howells struct rb_node *parent, **pp; 288999b69f8SDavid Howells int ret; 2898c3e34a4SDavid Howells 29048124178SDavid Howells _enter("%p,%lx", rx, p->user_call_ID); 2918c3e34a4SDavid Howells 292b7a7d674SDavid Howells limiter = rxrpc_get_call_slot(p, gfp); 293b0f571ecSDavid Howells if (!limiter) { 294b0f571ecSDavid Howells release_sock(&rx->sk); 295b7a7d674SDavid Howells return ERR_PTR(-ERESTARTSYS); 296b0f571ecSDavid Howells } 297b7a7d674SDavid Howells 298a25e21f0SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id); 2998c3e34a4SDavid Howells if (IS_ERR(call)) { 300540b1c48SDavid Howells release_sock(&rx->sk); 301b7a7d674SDavid Howells up(limiter); 3028c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 3038c3e34a4SDavid Howells return call; 3048c3e34a4SDavid Howells } 3058c3e34a4SDavid Howells 306e138aa7dSDavid Howells call->interruptibility = p->interruptibility; 30748124178SDavid Howells call->tx_total_len = p->tx_total_len; 308cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 309cb0fc0c9SDavid Howells p->user_call_ID, rxrpc_call_new_client); 310b7a7d674SDavid Howells if (p->kernel) 311b7a7d674SDavid Howells __set_bit(RXRPC_CALL_KERNEL, &call->flags); 312e34d4234SDavid Howells 313540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 314540b1c48SDavid Howells * will be acting outside the socket lock. 315540b1c48SDavid Howells */ 316540b1c48SDavid Howells mutex_lock(&call->user_mutex); 317540b1c48SDavid Howells 318999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 3198c3e34a4SDavid Howells write_lock(&rx->call_lock); 3208c3e34a4SDavid Howells 3218c3e34a4SDavid Howells pp = &rx->calls.rb_node; 3228c3e34a4SDavid Howells parent = NULL; 3238c3e34a4SDavid Howells while (*pp) { 3248c3e34a4SDavid Howells parent = *pp; 3258c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 3268c3e34a4SDavid Howells 32748124178SDavid Howells if (p->user_call_ID < xcall->user_call_ID) 3288c3e34a4SDavid Howells pp = &(*pp)->rb_left; 32948124178SDavid Howells else if (p->user_call_ID > xcall->user_call_ID) 3308c3e34a4SDavid Howells pp = &(*pp)->rb_right; 3318c3e34a4SDavid Howells else 332357f5ef6SDavid Howells goto error_dup_user_ID; 3338c3e34a4SDavid Howells } 3348c3e34a4SDavid Howells 335248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 33648124178SDavid Howells call->user_call_ID = p->user_call_ID; 337357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 338cb0fc0c9SDavid Howells rxrpc_get_call(call, rxrpc_call_get_userid); 3398c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 3408c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 341248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 342248f219cSDavid Howells 3438c3e34a4SDavid Howells write_unlock(&rx->call_lock); 3448c3e34a4SDavid Howells 345d3be4d24SDavid Howells rxnet = call->rxnet; 346ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 347ad25f5cbSDavid Howells list_add_tail_rcu(&call->link, &rxnet->calls); 348ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 3498c3e34a4SDavid Howells 350540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 351540b1c48SDavid Howells release_sock(&rx->sk); 352540b1c48SDavid Howells 353248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 354248f219cSDavid Howells * including channel number and call ID. 355248f219cSDavid Howells */ 3565e33a23bSDavid Howells ret = rxrpc_connect_call(rx, call, cp, srx, gfp); 357999b69f8SDavid Howells if (ret < 0) 35865550098SDavid Howells goto error_attached_to_socket; 359999b69f8SDavid Howells 360cb0fc0c9SDavid Howells rxrpc_see_call(call, rxrpc_call_see_connected); 361a84a46d7SDavid Howells 362248f219cSDavid Howells rxrpc_start_call_timer(call); 363248f219cSDavid Howells 3648c3e34a4SDavid Howells _leave(" = %p [new]", call); 3658c3e34a4SDavid Howells return call; 3668c3e34a4SDavid Howells 3678c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 3688c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 3698c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 3708c3e34a4SDavid Howells * same time in different threads. 3718c3e34a4SDavid Howells */ 372357f5ef6SDavid Howells error_dup_user_ID: 3738c3e34a4SDavid Howells write_unlock(&rx->call_lock); 374540b1c48SDavid Howells release_sock(&rx->sk); 375357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 37665550098SDavid Howells RX_CALL_DEAD, -EEXIST); 377cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 0, 378cb0fc0c9SDavid Howells rxrpc_call_see_userid_exists); 379357f5ef6SDavid Howells rxrpc_release_call(rx, call); 380540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 381cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid_exists); 38265550098SDavid Howells _leave(" = -EEXIST"); 38365550098SDavid Howells return ERR_PTR(-EEXIST); 38465550098SDavid Howells 38565550098SDavid Howells /* We got an error, but the call is attached to the socket and is in 38665550098SDavid Howells * need of release. However, we might now race with recvmsg() when 38765550098SDavid Howells * completing the call queues it. Return 0 from sys_sendmsg() and 38865550098SDavid Howells * leave the error to recvmsg() to deal with. 38965550098SDavid Howells */ 39065550098SDavid Howells error_attached_to_socket: 391cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), ret, 392cb0fc0c9SDavid Howells rxrpc_call_see_connect_failed); 39365550098SDavid Howells set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); 39465550098SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 39565550098SDavid Howells RX_CALL_DEAD, ret); 39665550098SDavid Howells _leave(" = c=%08x [err]", call->debug_id); 39765550098SDavid Howells return call; 3988c3e34a4SDavid Howells } 3998c3e34a4SDavid Howells 4008c3e34a4SDavid Howells /* 401248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 402248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 4038c3e34a4SDavid Howells */ 404248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 405248f219cSDavid Howells struct rxrpc_call *call, 40642886ffeSDavid Howells struct sk_buff *skb) 4078c3e34a4SDavid Howells { 408248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 40942886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 410248f219cSDavid Howells u32 chan; 4118c3e34a4SDavid Howells 412248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 4138c3e34a4SDavid Howells 414248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 415248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 416248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 417248f219cSDavid Howells call->cid = sp->hdr.cid; 418248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 41957494343SDavid Howells call->cong_tstamp = skb->tstamp; 4208c3e34a4SDavid Howells 421248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 422248f219cSDavid Howells * only defending against the data_ready handler (which we're called 423248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 424248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 425248f219cSDavid Howells * call pointer). 4268c3e34a4SDavid Howells */ 427248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 428248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 429248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 430a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 4318c3e34a4SDavid Howells 4322cc80086SDavid Howells spin_lock(&conn->peer->lock); 4332cc80086SDavid Howells hlist_add_head_rcu(&call->error_link, &conn->peer->error_targets); 4342cc80086SDavid Howells spin_unlock(&conn->peer->lock); 4358c3e34a4SDavid Howells 436248f219cSDavid Howells rxrpc_start_call_timer(call); 437248f219cSDavid Howells _leave(""); 4388c3e34a4SDavid Howells } 4398c3e34a4SDavid Howells 4408c3e34a4SDavid Howells /* 4413feda9d6SDavid Howells * Queue a call's work processor. 4428d94aa38SDavid Howells */ 4433feda9d6SDavid Howells void rxrpc_queue_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 4448d94aa38SDavid Howells { 4458d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 4463feda9d6SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 0, why); 4478d94aa38SDavid Howells } 4488d94aa38SDavid Howells 4498d94aa38SDavid Howells /* 450e34d4234SDavid Howells * Note the re-emergence of a call. 451e34d4234SDavid Howells */ 452cb0fc0c9SDavid Howells void rxrpc_see_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 453e34d4234SDavid Howells { 454e34d4234SDavid Howells if (call) { 455cb0fc0c9SDavid Howells int r = refcount_read(&call->ref); 456e34d4234SDavid Howells 457cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, r, 0, why); 458e34d4234SDavid Howells } 459e34d4234SDavid Howells } 460e34d4234SDavid Howells 461cb0fc0c9SDavid Howells bool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 4624a7f62f9SDavid Howells { 463cb0fc0c9SDavid Howells int r; 4644a7f62f9SDavid Howells 465cb0fc0c9SDavid Howells if (!__refcount_inc_not_zero(&call->ref, &r)) 4664a7f62f9SDavid Howells return false; 467cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, r + 1, 0, why); 4684a7f62f9SDavid Howells return true; 4694a7f62f9SDavid Howells } 4704a7f62f9SDavid Howells 471e34d4234SDavid Howells /* 472e34d4234SDavid Howells * Note the addition of a ref on a call. 473e34d4234SDavid Howells */ 474cb0fc0c9SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 475e34d4234SDavid Howells { 476cb0fc0c9SDavid Howells int r; 477e34d4234SDavid Howells 478cb0fc0c9SDavid Howells __refcount_inc(&call->ref, &r); 479cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, r + 1, 0, why); 480e34d4234SDavid Howells } 481e34d4234SDavid Howells 482e34d4234SDavid Howells /* 483a4ea4c47SDavid Howells * Clean up the Rx skb ring. 484a641fd00SDavid Howells */ 485a641fd00SDavid Howells static void rxrpc_cleanup_ring(struct rxrpc_call *call) 486a641fd00SDavid Howells { 4875d7edbc9SDavid Howells skb_queue_purge(&call->recvmsg_queue); 4885d7edbc9SDavid Howells skb_queue_purge(&call->rx_oos_queue); 489a641fd00SDavid Howells } 490a641fd00SDavid Howells 491a641fd00SDavid Howells /* 492248f219cSDavid Howells * Detach a call from its owning socket. 4938c3e34a4SDavid Howells */ 4948d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 4958c3e34a4SDavid Howells { 496248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 497248f219cSDavid Howells bool put = false; 498248f219cSDavid Howells 499a0575429SDavid Howells _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref)); 500248f219cSDavid Howells 501cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 502cb0fc0c9SDavid Howells call->flags, rxrpc_call_see_release); 5038c3e34a4SDavid Howells 504a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 505e34d4234SDavid Howells 5068c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 5078c3e34a4SDavid Howells BUG(); 5088c3e34a4SDavid Howells 509b7a7d674SDavid Howells rxrpc_put_call_slot(call); 5103feda9d6SDavid Howells del_timer_sync(&call->timer); 5118c3e34a4SDavid Howells 512248f219cSDavid Howells /* Make sure we don't get any more notifications */ 513248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 514e653cfe4SDavid Howells 515248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 5168c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 5178c3e34a4SDavid Howells call, call->events, call->flags); 518248f219cSDavid Howells list_del(&call->recvmsg_link); 519248f219cSDavid Howells put = true; 520248f219cSDavid Howells } 521248f219cSDavid Howells 522248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 523248f219cSDavid Howells call->recvmsg_link.next = NULL; 524248f219cSDavid Howells call->recvmsg_link.prev = NULL; 525248f219cSDavid Howells 526248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 527248f219cSDavid Howells if (put) 528cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_unnotify); 529248f219cSDavid Howells 530248f219cSDavid Howells write_lock(&rx->call_lock); 531248f219cSDavid Howells 532248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 5338c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 5348c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 535cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid_exists); 5368c3e34a4SDavid Howells } 5378c3e34a4SDavid Howells 538248f219cSDavid Howells list_del(&call->sock_link); 539248f219cSDavid Howells write_unlock(&rx->call_lock); 5408c3e34a4SDavid Howells 541248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 5428c3e34a4SDavid Howells 5435273a191SDavid Howells if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 544e653cfe4SDavid Howells rxrpc_disconnect_call(call); 54591fcfbe8SDavid Howells if (call->security) 54691fcfbe8SDavid Howells call->security->free_call_crypto(call); 5478c3e34a4SDavid Howells _leave(""); 5488c3e34a4SDavid Howells } 5498c3e34a4SDavid Howells 5508c3e34a4SDavid Howells /* 5518c3e34a4SDavid Howells * release all the calls associated with a socket 5528c3e34a4SDavid Howells */ 5538c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 5548c3e34a4SDavid Howells { 5558c3e34a4SDavid Howells struct rxrpc_call *call; 5568c3e34a4SDavid Howells 5578c3e34a4SDavid Howells _enter("%p", rx); 5588c3e34a4SDavid Howells 5590360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 5600360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 5610360da6dSDavid Howells struct rxrpc_call, accept_link); 5620360da6dSDavid Howells list_del(&call->accept_link); 5633a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 564cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_release_sock_tba); 5650360da6dSDavid Howells } 5660360da6dSDavid Howells 567248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 568248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 569248f219cSDavid Howells struct rxrpc_call, sock_link); 570cb0fc0c9SDavid Howells rxrpc_get_call(call, rxrpc_call_get_release_sock); 5713a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 57226cb02aaSDavid Howells rxrpc_send_abort_packet(call); 5738d94aa38SDavid Howells rxrpc_release_call(rx, call); 574cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_release_sock); 5758c3e34a4SDavid Howells } 5768c3e34a4SDavid Howells 5778c3e34a4SDavid Howells _leave(""); 5788c3e34a4SDavid Howells } 5798c3e34a4SDavid Howells 5808c3e34a4SDavid Howells /* 5818c3e34a4SDavid Howells * release a call 5828c3e34a4SDavid Howells */ 583cb0fc0c9SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 5848c3e34a4SDavid Howells { 585d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 58648c9e0ecSDavid Howells unsigned int debug_id = call->debug_id; 587a0575429SDavid Howells bool dead; 588cb0fc0c9SDavid Howells int r; 589e34d4234SDavid Howells 5908c3e34a4SDavid Howells ASSERT(call != NULL); 5918c3e34a4SDavid Howells 592cb0fc0c9SDavid Howells dead = __refcount_dec_and_test(&call->ref, &r); 593cb0fc0c9SDavid Howells trace_rxrpc_call(debug_id, r - 1, 0, why); 594a0575429SDavid Howells if (dead) { 595248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 596e34d4234SDavid Howells 5972baec2c3SDavid Howells if (!list_empty(&call->link)) { 598ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 599248f219cSDavid Howells list_del_init(&call->link); 600ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 6012baec2c3SDavid Howells } 602e34d4234SDavid Howells 6038d94aa38SDavid Howells rxrpc_cleanup_call(call); 604e34d4234SDavid Howells } 6058c3e34a4SDavid Howells } 6068c3e34a4SDavid Howells 6078c3e34a4SDavid Howells /* 6083feda9d6SDavid Howells * Free up the call under RCU. 609dee46364SDavid Howells */ 6103feda9d6SDavid Howells static void rxrpc_rcu_free_call(struct rcu_head *rcu) 611dee46364SDavid Howells { 6123feda9d6SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 6133feda9d6SDavid Howells struct rxrpc_net *rxnet = READ_ONCE(call->rxnet); 614dee46364SDavid Howells 615dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 616d3be4d24SDavid Howells if (atomic_dec_and_test(&rxnet->nr_calls)) 6175bb053beSLinus Torvalds wake_up_var(&rxnet->nr_calls); 618dee46364SDavid Howells } 619dee46364SDavid Howells 620dee46364SDavid Howells /* 6213feda9d6SDavid Howells * Final call destruction - but must be done in process context. 622963485d4SDavid Howells */ 6233feda9d6SDavid Howells static void rxrpc_destroy_call(struct work_struct *work) 624963485d4SDavid Howells { 6253feda9d6SDavid Howells struct rxrpc_call *call = container_of(work, struct rxrpc_call, destroyer); 626a4ea4c47SDavid Howells struct rxrpc_txbuf *txb; 627a4ea4c47SDavid Howells 6283feda9d6SDavid Howells del_timer_sync(&call->timer); 6293feda9d6SDavid Howells cancel_work_sync(&call->processor); /* The processor may restart the timer */ 6303feda9d6SDavid Howells del_timer_sync(&call->timer); 6318c3e34a4SDavid Howells 632a641fd00SDavid Howells rxrpc_cleanup_ring(call); 633a4ea4c47SDavid Howells while ((txb = list_first_entry_or_null(&call->tx_buffer, 634a4ea4c47SDavid Howells struct rxrpc_txbuf, call_link))) { 635a4ea4c47SDavid Howells list_del(&txb->call_link); 636a4ea4c47SDavid Howells rxrpc_put_txbuf(txb, rxrpc_txbuf_put_cleaned); 637a4ea4c47SDavid Howells } 638a4ea4c47SDavid Howells rxrpc_put_txbuf(call->tx_pending, rxrpc_txbuf_put_cleaned); 6399a36a6bcSDavid Howells rxrpc_free_skb(call->acks_soft_tbl, rxrpc_skb_put_ack); 6403feda9d6SDavid Howells rxrpc_put_connection(call->conn, rxrpc_conn_put_call); 6413feda9d6SDavid Howells rxrpc_put_peer(call->peer, rxrpc_peer_put_call); 6423feda9d6SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_free_call); 6433feda9d6SDavid Howells } 6448c3e34a4SDavid Howells 6453feda9d6SDavid Howells /* 6463feda9d6SDavid Howells * clean up a call 6473feda9d6SDavid Howells */ 6483feda9d6SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 6493feda9d6SDavid Howells { 6503feda9d6SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 6513feda9d6SDavid Howells 6523feda9d6SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 6533feda9d6SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 6543feda9d6SDavid Howells 6553feda9d6SDavid Howells del_timer_sync(&call->timer); 6563feda9d6SDavid Howells cancel_work(&call->processor); 6573feda9d6SDavid Howells 658446b3e14SDavid Howells if (rcu_read_lock_held() || work_busy(&call->processor)) 6593feda9d6SDavid Howells /* Can't use the rxrpc workqueue as we need to cancel/flush 6603feda9d6SDavid Howells * something that may be running/waiting there. 6613feda9d6SDavid Howells */ 6623feda9d6SDavid Howells schedule_work(&call->destroyer); 6633feda9d6SDavid Howells else 6643feda9d6SDavid Howells rxrpc_destroy_call(&call->destroyer); 6658c3e34a4SDavid Howells } 6668c3e34a4SDavid Howells 6678c3e34a4SDavid Howells /* 6682baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 6692baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 6702baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 6718c3e34a4SDavid Howells */ 6722baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 6738c3e34a4SDavid Howells { 6748c3e34a4SDavid Howells struct rxrpc_call *call; 6758c3e34a4SDavid Howells 6768c3e34a4SDavid Howells _enter(""); 6778d94aa38SDavid Howells 678b1302342SDavid Howells if (!list_empty(&rxnet->calls)) { 679ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 6808c3e34a4SDavid Howells 6812baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 682b1302342SDavid Howells call = list_entry(rxnet->calls.next, 683b1302342SDavid Howells struct rxrpc_call, link); 6848c3e34a4SDavid Howells _debug("Zapping call %p", call); 6858c3e34a4SDavid Howells 686cb0fc0c9SDavid Howells rxrpc_see_call(call, rxrpc_call_see_zap); 6878c3e34a4SDavid Howells list_del_init(&call->link); 6888c3e34a4SDavid Howells 689248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 690a0575429SDavid Howells call, refcount_read(&call->ref), 6918c3e34a4SDavid Howells rxrpc_call_states[call->state], 6928c3e34a4SDavid Howells call->flags, call->events); 6938c3e34a4SDavid Howells 694ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 6958c3e34a4SDavid Howells cond_resched(); 696ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 6978c3e34a4SDavid Howells } 6988c3e34a4SDavid Howells 699ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 700b1302342SDavid Howells } 701d3be4d24SDavid Howells 702d3be4d24SDavid Howells atomic_dec(&rxnet->nr_calls); 7035bb053beSLinus Torvalds wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls)); 7048c3e34a4SDavid Howells } 705