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); 56*3feda9d6SDavid Howells rxrpc_queue_call(call, rxrpc_call_queue_timer); 57fc7ab6d2SDavid Howells } 58248f219cSDavid Howells } 598c3e34a4SDavid Howells 604a7f62f9SDavid Howells void rxrpc_reduce_call_timer(struct rxrpc_call *call, 614a7f62f9SDavid Howells unsigned long expire_at, 624a7f62f9SDavid Howells unsigned long now, 634a7f62f9SDavid Howells enum rxrpc_timer_trace why) 644a7f62f9SDavid Howells { 654a7f62f9SDavid Howells trace_rxrpc_timer(call, why, now); 66*3feda9d6SDavid Howells timer_reduce(&call->timer, expire_at); 674a7f62f9SDavid Howells } 684a7f62f9SDavid Howells 699faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 708c3e34a4SDavid Howells 71*3feda9d6SDavid Howells static void rxrpc_destroy_call(struct work_struct *); 72*3feda9d6SDavid Howells 738c3e34a4SDavid Howells /* 748c3e34a4SDavid Howells * find an extant server call 758c3e34a4SDavid Howells * - called in process context with IRQs enabled 768c3e34a4SDavid Howells */ 778c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 788c3e34a4SDavid Howells unsigned long user_call_ID) 798c3e34a4SDavid Howells { 808c3e34a4SDavid Howells struct rxrpc_call *call; 818c3e34a4SDavid Howells struct rb_node *p; 828c3e34a4SDavid Howells 838c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 848c3e34a4SDavid Howells 858c3e34a4SDavid Howells read_lock(&rx->call_lock); 868c3e34a4SDavid Howells 878c3e34a4SDavid Howells p = rx->calls.rb_node; 888c3e34a4SDavid Howells while (p) { 898c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 908c3e34a4SDavid Howells 918c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 928c3e34a4SDavid Howells p = p->rb_left; 938c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 948c3e34a4SDavid Howells p = p->rb_right; 958c3e34a4SDavid Howells else 968c3e34a4SDavid Howells goto found_extant_call; 978c3e34a4SDavid Howells } 988c3e34a4SDavid Howells 998c3e34a4SDavid Howells read_unlock(&rx->call_lock); 1008c3e34a4SDavid Howells _leave(" = NULL"); 1018c3e34a4SDavid Howells return NULL; 1028c3e34a4SDavid Howells 1038c3e34a4SDavid Howells found_extant_call: 104cb0fc0c9SDavid Howells rxrpc_get_call(call, rxrpc_call_get_sendmsg); 1058c3e34a4SDavid Howells read_unlock(&rx->call_lock); 106a0575429SDavid Howells _leave(" = %p [%d]", call, refcount_read(&call->ref)); 1078c3e34a4SDavid Howells return call; 1088c3e34a4SDavid Howells } 1098c3e34a4SDavid Howells 1108c3e34a4SDavid Howells /* 1118c3e34a4SDavid Howells * allocate a new call 1128c3e34a4SDavid Howells */ 113a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp, 114a25e21f0SDavid Howells unsigned int debug_id) 1158c3e34a4SDavid Howells { 1168c3e34a4SDavid Howells struct rxrpc_call *call; 117d3be4d24SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 1188c3e34a4SDavid Howells 1198c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1208c3e34a4SDavid Howells if (!call) 1218c3e34a4SDavid Howells return NULL; 1228c3e34a4SDavid Howells 123540b1c48SDavid Howells mutex_init(&call->user_mutex); 1249faaff59SDavid Howells 1259faaff59SDavid Howells /* Prevent lockdep reporting a deadlock false positive between the afs 1269faaff59SDavid Howells * filesystem and sys_sendmsg() via the mmap sem. 1279faaff59SDavid Howells */ 1289faaff59SDavid Howells if (rx->sk.sk_kern_sock) 1299faaff59SDavid Howells lockdep_set_class(&call->user_mutex, 1309faaff59SDavid Howells &rxrpc_call_user_mutex_lock_class_key); 1319faaff59SDavid Howells 132e99e88a9SKees Cook timer_setup(&call->timer, rxrpc_call_timer_expired, 0); 133*3feda9d6SDavid Howells INIT_WORK(&call->processor, rxrpc_process_call); 134*3feda9d6SDavid Howells INIT_WORK(&call->destroyer, rxrpc_destroy_call); 135999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 13645025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1378c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 138248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 139248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 140a4ea4c47SDavid Howells INIT_LIST_HEAD(&call->tx_buffer); 1415d7edbc9SDavid Howells skb_queue_head_init(&call->recvmsg_queue); 1425d7edbc9SDavid Howells skb_queue_head_init(&call->rx_oos_queue); 14345025bceSDavid Howells init_waitqueue_head(&call->waitq); 14420acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 145a4ea4c47SDavid Howells spin_lock_init(&call->tx_lock); 146c1e15b49SDavid Howells spin_lock_init(&call->input_lock); 147d57a3a15SDavid Howells spin_lock_init(&call->acks_ack_lock); 1488c3e34a4SDavid Howells rwlock_init(&call->state_lock); 149a0575429SDavid Howells refcount_set(&call->ref, 1); 150a25e21f0SDavid Howells call->debug_id = debug_id; 151e754eba6SDavid Howells call->tx_total_len = -1; 152a158bdd3SDavid Howells call->next_rx_timo = 20 * HZ; 153a158bdd3SDavid Howells call->next_req_timo = 1 * HZ; 1545d7edbc9SDavid Howells atomic64_set(&call->ackr_window, 0x100000001ULL); 1558c3e34a4SDavid Howells 1568c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1578c3e34a4SDavid Howells 15875e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 159248f219cSDavid Howells call->tx_winsize = 16; 16057494343SDavid Howells 1611fc4fa2aSDavid Howells if (RXRPC_TX_SMSS > 2190) 16257494343SDavid Howells call->cong_cwnd = 2; 1631fc4fa2aSDavid Howells else if (RXRPC_TX_SMSS > 1095) 1641fc4fa2aSDavid Howells call->cong_cwnd = 3; 1651fc4fa2aSDavid Howells else 1661fc4fa2aSDavid Howells call->cong_cwnd = 4; 167a4ea4c47SDavid Howells call->cong_ssthresh = RXRPC_TX_MAX_WINDOW; 168d3be4d24SDavid Howells 169d3be4d24SDavid Howells call->rxnet = rxnet; 1704700c4d8SDavid Howells call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK; 171d3be4d24SDavid Howells atomic_inc(&rxnet->nr_calls); 1728c3e34a4SDavid Howells return call; 1738c3e34a4SDavid Howells } 1748c3e34a4SDavid Howells 1758c3e34a4SDavid Howells /* 176999b69f8SDavid Howells * Allocate a new client call. 1778c3e34a4SDavid Howells */ 1789faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 1799faaff59SDavid Howells struct sockaddr_rxrpc *srx, 180a25e21f0SDavid Howells gfp_t gfp, 181a25e21f0SDavid Howells unsigned int debug_id) 1828c3e34a4SDavid Howells { 1838c3e34a4SDavid Howells struct rxrpc_call *call; 18457494343SDavid Howells ktime_t now; 1858c3e34a4SDavid Howells 1868c3e34a4SDavid Howells _enter(""); 1878c3e34a4SDavid Howells 188a25e21f0SDavid Howells call = rxrpc_alloc_call(rx, gfp, debug_id); 1898c3e34a4SDavid Howells if (!call) 1908c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 191999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 192999b69f8SDavid Howells call->service_id = srx->srx_service; 19357494343SDavid Howells now = ktime_get_real(); 19457494343SDavid Howells call->acks_latest_ts = now; 19557494343SDavid Howells call->cong_tstamp = now; 196999b69f8SDavid Howells 197999b69f8SDavid Howells _leave(" = %p", call); 198999b69f8SDavid Howells return call; 199999b69f8SDavid Howells } 200999b69f8SDavid Howells 201999b69f8SDavid Howells /* 202248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 203999b69f8SDavid Howells */ 204248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 205999b69f8SDavid Howells { 206a158bdd3SDavid Howells unsigned long now = jiffies; 207a158bdd3SDavid Howells unsigned long j = now + MAX_JIFFY_OFFSET; 208999b69f8SDavid Howells 209530403d9SDavid Howells call->delay_ack_at = j; 210bd1fdf8cSDavid Howells call->ack_lost_at = j; 211a158bdd3SDavid Howells call->resend_at = j; 212a158bdd3SDavid Howells call->ping_at = j; 213a158bdd3SDavid Howells call->expect_rx_by = j; 214a158bdd3SDavid Howells call->expect_req_by = j; 215a158bdd3SDavid Howells call->expect_term_by = j; 216a158bdd3SDavid Howells call->timer.expires = now; 2178c3e34a4SDavid Howells } 2188c3e34a4SDavid Howells 2198c3e34a4SDavid Howells /* 220b7a7d674SDavid Howells * Wait for a call slot to become available. 221b7a7d674SDavid Howells */ 222b7a7d674SDavid Howells static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp) 223b7a7d674SDavid Howells { 224b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 225b7a7d674SDavid Howells 226b7a7d674SDavid Howells if (p->kernel) 227b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 228b7a7d674SDavid Howells if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) { 229b7a7d674SDavid Howells down(limiter); 230b7a7d674SDavid Howells return limiter; 231b7a7d674SDavid Howells } 232b7a7d674SDavid Howells return down_interruptible(limiter) < 0 ? NULL : limiter; 233b7a7d674SDavid Howells } 234b7a7d674SDavid Howells 235b7a7d674SDavid Howells /* 236b7a7d674SDavid Howells * Release a call slot. 237b7a7d674SDavid Howells */ 238b7a7d674SDavid Howells static void rxrpc_put_call_slot(struct rxrpc_call *call) 239b7a7d674SDavid Howells { 240b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 241b7a7d674SDavid Howells 242b7a7d674SDavid Howells if (test_bit(RXRPC_CALL_KERNEL, &call->flags)) 243b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 244b7a7d674SDavid Howells up(limiter); 245b7a7d674SDavid Howells } 246b7a7d674SDavid Howells 247b7a7d674SDavid Howells /* 248540b1c48SDavid Howells * Set up a call for the given parameters. 249540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 250540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2518c3e34a4SDavid Howells */ 2528c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 25319ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 254999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 25548124178SDavid Howells struct rxrpc_call_params *p, 256a25e21f0SDavid Howells gfp_t gfp, 257a25e21f0SDavid Howells unsigned int debug_id) 258540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 25988f2a825SDavid Howells __acquires(&call->user_mutex) 2608c3e34a4SDavid Howells { 2618c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 262d3be4d24SDavid Howells struct rxrpc_net *rxnet; 263b7a7d674SDavid Howells struct semaphore *limiter; 2648c3e34a4SDavid Howells struct rb_node *parent, **pp; 265999b69f8SDavid Howells int ret; 2668c3e34a4SDavid Howells 26748124178SDavid Howells _enter("%p,%lx", rx, p->user_call_ID); 2688c3e34a4SDavid Howells 269b7a7d674SDavid Howells limiter = rxrpc_get_call_slot(p, gfp); 270b0f571ecSDavid Howells if (!limiter) { 271b0f571ecSDavid Howells release_sock(&rx->sk); 272b7a7d674SDavid Howells return ERR_PTR(-ERESTARTSYS); 273b0f571ecSDavid Howells } 274b7a7d674SDavid Howells 275a25e21f0SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id); 2768c3e34a4SDavid Howells if (IS_ERR(call)) { 277540b1c48SDavid Howells release_sock(&rx->sk); 278b7a7d674SDavid Howells up(limiter); 2798c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 2808c3e34a4SDavid Howells return call; 2818c3e34a4SDavid Howells } 2828c3e34a4SDavid Howells 283e138aa7dSDavid Howells call->interruptibility = p->interruptibility; 28448124178SDavid Howells call->tx_total_len = p->tx_total_len; 285cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 286cb0fc0c9SDavid Howells p->user_call_ID, rxrpc_call_new_client); 287b7a7d674SDavid Howells if (p->kernel) 288b7a7d674SDavid Howells __set_bit(RXRPC_CALL_KERNEL, &call->flags); 289e34d4234SDavid Howells 290540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 291540b1c48SDavid Howells * will be acting outside the socket lock. 292540b1c48SDavid Howells */ 293540b1c48SDavid Howells mutex_lock(&call->user_mutex); 294540b1c48SDavid Howells 295999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 2968c3e34a4SDavid Howells write_lock(&rx->call_lock); 2978c3e34a4SDavid Howells 2988c3e34a4SDavid Howells pp = &rx->calls.rb_node; 2998c3e34a4SDavid Howells parent = NULL; 3008c3e34a4SDavid Howells while (*pp) { 3018c3e34a4SDavid Howells parent = *pp; 3028c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 3038c3e34a4SDavid Howells 30448124178SDavid Howells if (p->user_call_ID < xcall->user_call_ID) 3058c3e34a4SDavid Howells pp = &(*pp)->rb_left; 30648124178SDavid Howells else if (p->user_call_ID > xcall->user_call_ID) 3078c3e34a4SDavid Howells pp = &(*pp)->rb_right; 3088c3e34a4SDavid Howells else 309357f5ef6SDavid Howells goto error_dup_user_ID; 3108c3e34a4SDavid Howells } 3118c3e34a4SDavid Howells 312248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 31348124178SDavid Howells call->user_call_ID = p->user_call_ID; 314357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 315cb0fc0c9SDavid Howells rxrpc_get_call(call, rxrpc_call_get_userid); 3168c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 3178c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 318248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 319248f219cSDavid Howells 3208c3e34a4SDavid Howells write_unlock(&rx->call_lock); 3218c3e34a4SDavid Howells 322d3be4d24SDavid Howells rxnet = call->rxnet; 323ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 324ad25f5cbSDavid Howells list_add_tail_rcu(&call->link, &rxnet->calls); 325ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 3268c3e34a4SDavid Howells 327540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 328540b1c48SDavid Howells release_sock(&rx->sk); 329540b1c48SDavid Howells 330248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 331248f219cSDavid Howells * including channel number and call ID. 332248f219cSDavid Howells */ 3335e33a23bSDavid Howells ret = rxrpc_connect_call(rx, call, cp, srx, gfp); 334999b69f8SDavid Howells if (ret < 0) 33565550098SDavid Howells goto error_attached_to_socket; 336999b69f8SDavid Howells 337cb0fc0c9SDavid Howells rxrpc_see_call(call, rxrpc_call_see_connected); 338a84a46d7SDavid Howells 339248f219cSDavid Howells rxrpc_start_call_timer(call); 340248f219cSDavid Howells 3418c3e34a4SDavid Howells _leave(" = %p [new]", call); 3428c3e34a4SDavid Howells return call; 3438c3e34a4SDavid Howells 3448c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 3458c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 3468c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 3478c3e34a4SDavid Howells * same time in different threads. 3488c3e34a4SDavid Howells */ 349357f5ef6SDavid Howells error_dup_user_ID: 3508c3e34a4SDavid Howells write_unlock(&rx->call_lock); 351540b1c48SDavid Howells release_sock(&rx->sk); 352357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 35365550098SDavid Howells RX_CALL_DEAD, -EEXIST); 354cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 0, 355cb0fc0c9SDavid Howells rxrpc_call_see_userid_exists); 356357f5ef6SDavid Howells rxrpc_release_call(rx, call); 357540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 358cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid_exists); 35965550098SDavid Howells _leave(" = -EEXIST"); 36065550098SDavid Howells return ERR_PTR(-EEXIST); 36165550098SDavid Howells 36265550098SDavid Howells /* We got an error, but the call is attached to the socket and is in 36365550098SDavid Howells * need of release. However, we might now race with recvmsg() when 36465550098SDavid Howells * completing the call queues it. Return 0 from sys_sendmsg() and 36565550098SDavid Howells * leave the error to recvmsg() to deal with. 36665550098SDavid Howells */ 36765550098SDavid Howells error_attached_to_socket: 368cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), ret, 369cb0fc0c9SDavid Howells rxrpc_call_see_connect_failed); 37065550098SDavid Howells set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); 37165550098SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 37265550098SDavid Howells RX_CALL_DEAD, ret); 37365550098SDavid Howells _leave(" = c=%08x [err]", call->debug_id); 37465550098SDavid Howells return call; 3758c3e34a4SDavid Howells } 3768c3e34a4SDavid Howells 3778c3e34a4SDavid Howells /* 378248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 379248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 3808c3e34a4SDavid Howells */ 381248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 382248f219cSDavid Howells struct rxrpc_call *call, 38342886ffeSDavid Howells struct sk_buff *skb) 3848c3e34a4SDavid Howells { 385248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 38642886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 387248f219cSDavid Howells u32 chan; 3888c3e34a4SDavid Howells 389248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 3908c3e34a4SDavid Howells 391248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 392248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 393248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 394248f219cSDavid Howells call->cid = sp->hdr.cid; 395248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 39657494343SDavid Howells call->cong_tstamp = skb->tstamp; 3978c3e34a4SDavid Howells 398248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 399248f219cSDavid Howells * only defending against the data_ready handler (which we're called 400248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 401248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 402248f219cSDavid Howells * call pointer). 4038c3e34a4SDavid Howells */ 404248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 405248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 406248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 407a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 4088c3e34a4SDavid Howells 4092cc80086SDavid Howells spin_lock(&conn->peer->lock); 4102cc80086SDavid Howells hlist_add_head_rcu(&call->error_link, &conn->peer->error_targets); 4112cc80086SDavid Howells spin_unlock(&conn->peer->lock); 4128c3e34a4SDavid Howells 413248f219cSDavid Howells rxrpc_start_call_timer(call); 414248f219cSDavid Howells _leave(""); 4158c3e34a4SDavid Howells } 4168c3e34a4SDavid Howells 4178c3e34a4SDavid Howells /* 418*3feda9d6SDavid Howells * Queue a call's work processor. 4198d94aa38SDavid Howells */ 420*3feda9d6SDavid Howells void rxrpc_queue_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 4218d94aa38SDavid Howells { 4228d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 423*3feda9d6SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 0, why); 4248d94aa38SDavid Howells } 4258d94aa38SDavid Howells 4268d94aa38SDavid Howells /* 427e34d4234SDavid Howells * Note the re-emergence of a call. 428e34d4234SDavid Howells */ 429cb0fc0c9SDavid Howells void rxrpc_see_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 430e34d4234SDavid Howells { 431e34d4234SDavid Howells if (call) { 432cb0fc0c9SDavid Howells int r = refcount_read(&call->ref); 433e34d4234SDavid Howells 434cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, r, 0, why); 435e34d4234SDavid Howells } 436e34d4234SDavid Howells } 437e34d4234SDavid Howells 438cb0fc0c9SDavid Howells bool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 4394a7f62f9SDavid Howells { 440cb0fc0c9SDavid Howells int r; 4414a7f62f9SDavid Howells 442cb0fc0c9SDavid Howells if (!__refcount_inc_not_zero(&call->ref, &r)) 4434a7f62f9SDavid Howells return false; 444cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, r + 1, 0, why); 4454a7f62f9SDavid Howells return true; 4464a7f62f9SDavid Howells } 4474a7f62f9SDavid Howells 448e34d4234SDavid Howells /* 449e34d4234SDavid Howells * Note the addition of a ref on a call. 450e34d4234SDavid Howells */ 451cb0fc0c9SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 452e34d4234SDavid Howells { 453cb0fc0c9SDavid Howells int r; 454e34d4234SDavid Howells 455cb0fc0c9SDavid Howells __refcount_inc(&call->ref, &r); 456cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, r + 1, 0, why); 457e34d4234SDavid Howells } 458e34d4234SDavid Howells 459e34d4234SDavid Howells /* 460a4ea4c47SDavid Howells * Clean up the Rx skb ring. 461a641fd00SDavid Howells */ 462a641fd00SDavid Howells static void rxrpc_cleanup_ring(struct rxrpc_call *call) 463a641fd00SDavid Howells { 4645d7edbc9SDavid Howells skb_queue_purge(&call->recvmsg_queue); 4655d7edbc9SDavid Howells skb_queue_purge(&call->rx_oos_queue); 466a641fd00SDavid Howells } 467a641fd00SDavid Howells 468a641fd00SDavid Howells /* 469248f219cSDavid Howells * Detach a call from its owning socket. 4708c3e34a4SDavid Howells */ 4718d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 4728c3e34a4SDavid Howells { 473248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 474248f219cSDavid Howells bool put = false; 475248f219cSDavid Howells 476a0575429SDavid Howells _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref)); 477248f219cSDavid Howells 478cb0fc0c9SDavid Howells trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 479cb0fc0c9SDavid Howells call->flags, rxrpc_call_see_release); 4808c3e34a4SDavid Howells 481a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 482e34d4234SDavid Howells 4838c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 4848c3e34a4SDavid Howells BUG(); 4858c3e34a4SDavid Howells 486b7a7d674SDavid Howells rxrpc_put_call_slot(call); 487*3feda9d6SDavid Howells del_timer_sync(&call->timer); 4888c3e34a4SDavid Howells 489248f219cSDavid Howells /* Make sure we don't get any more notifications */ 490248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 491e653cfe4SDavid Howells 492248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 4938c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 4948c3e34a4SDavid Howells call, call->events, call->flags); 495248f219cSDavid Howells list_del(&call->recvmsg_link); 496248f219cSDavid Howells put = true; 497248f219cSDavid Howells } 498248f219cSDavid Howells 499248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 500248f219cSDavid Howells call->recvmsg_link.next = NULL; 501248f219cSDavid Howells call->recvmsg_link.prev = NULL; 502248f219cSDavid Howells 503248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 504248f219cSDavid Howells if (put) 505cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_unnotify); 506248f219cSDavid Howells 507248f219cSDavid Howells write_lock(&rx->call_lock); 508248f219cSDavid Howells 509248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 5108c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 5118c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 512cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid_exists); 5138c3e34a4SDavid Howells } 5148c3e34a4SDavid Howells 515248f219cSDavid Howells list_del(&call->sock_link); 516248f219cSDavid Howells write_unlock(&rx->call_lock); 5178c3e34a4SDavid Howells 518248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 5198c3e34a4SDavid Howells 5205273a191SDavid Howells if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 521e653cfe4SDavid Howells rxrpc_disconnect_call(call); 52291fcfbe8SDavid Howells if (call->security) 52391fcfbe8SDavid Howells call->security->free_call_crypto(call); 5248c3e34a4SDavid Howells _leave(""); 5258c3e34a4SDavid Howells } 5268c3e34a4SDavid Howells 5278c3e34a4SDavid Howells /* 5288c3e34a4SDavid Howells * release all the calls associated with a socket 5298c3e34a4SDavid Howells */ 5308c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 5318c3e34a4SDavid Howells { 5328c3e34a4SDavid Howells struct rxrpc_call *call; 5338c3e34a4SDavid Howells 5348c3e34a4SDavid Howells _enter("%p", rx); 5358c3e34a4SDavid Howells 5360360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 5370360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 5380360da6dSDavid Howells struct rxrpc_call, accept_link); 5390360da6dSDavid Howells list_del(&call->accept_link); 5403a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 541cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_release_sock_tba); 5420360da6dSDavid Howells } 5430360da6dSDavid Howells 544248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 545248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 546248f219cSDavid Howells struct rxrpc_call, sock_link); 547cb0fc0c9SDavid Howells rxrpc_get_call(call, rxrpc_call_get_release_sock); 5483a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 54926cb02aaSDavid Howells rxrpc_send_abort_packet(call); 5508d94aa38SDavid Howells rxrpc_release_call(rx, call); 551cb0fc0c9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_release_sock); 5528c3e34a4SDavid Howells } 5538c3e34a4SDavid Howells 5548c3e34a4SDavid Howells _leave(""); 5558c3e34a4SDavid Howells } 5568c3e34a4SDavid Howells 5578c3e34a4SDavid Howells /* 5588c3e34a4SDavid Howells * release a call 5598c3e34a4SDavid Howells */ 560cb0fc0c9SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace why) 5618c3e34a4SDavid Howells { 562d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 56348c9e0ecSDavid Howells unsigned int debug_id = call->debug_id; 564a0575429SDavid Howells bool dead; 565cb0fc0c9SDavid Howells int r; 566e34d4234SDavid Howells 5678c3e34a4SDavid Howells ASSERT(call != NULL); 5688c3e34a4SDavid Howells 569cb0fc0c9SDavid Howells dead = __refcount_dec_and_test(&call->ref, &r); 570cb0fc0c9SDavid Howells trace_rxrpc_call(debug_id, r - 1, 0, why); 571a0575429SDavid Howells if (dead) { 572248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 573e34d4234SDavid Howells 5742baec2c3SDavid Howells if (!list_empty(&call->link)) { 575ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 576248f219cSDavid Howells list_del_init(&call->link); 577ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 5782baec2c3SDavid Howells } 579e34d4234SDavid Howells 5808d94aa38SDavid Howells rxrpc_cleanup_call(call); 581e34d4234SDavid Howells } 5828c3e34a4SDavid Howells } 5838c3e34a4SDavid Howells 5848c3e34a4SDavid Howells /* 585*3feda9d6SDavid Howells * Free up the call under RCU. 586dee46364SDavid Howells */ 587*3feda9d6SDavid Howells static void rxrpc_rcu_free_call(struct rcu_head *rcu) 588dee46364SDavid Howells { 589*3feda9d6SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 590*3feda9d6SDavid Howells struct rxrpc_net *rxnet = READ_ONCE(call->rxnet); 591dee46364SDavid Howells 592dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 593d3be4d24SDavid Howells if (atomic_dec_and_test(&rxnet->nr_calls)) 5945bb053beSLinus Torvalds wake_up_var(&rxnet->nr_calls); 595dee46364SDavid Howells } 596dee46364SDavid Howells 597dee46364SDavid Howells /* 598*3feda9d6SDavid Howells * Final call destruction - but must be done in process context. 599963485d4SDavid Howells */ 600*3feda9d6SDavid Howells static void rxrpc_destroy_call(struct work_struct *work) 601963485d4SDavid Howells { 602*3feda9d6SDavid Howells struct rxrpc_call *call = container_of(work, struct rxrpc_call, destroyer); 603a4ea4c47SDavid Howells struct rxrpc_txbuf *txb; 604a4ea4c47SDavid Howells 605*3feda9d6SDavid Howells del_timer_sync(&call->timer); 606*3feda9d6SDavid Howells cancel_work_sync(&call->processor); /* The processor may restart the timer */ 607*3feda9d6SDavid Howells del_timer_sync(&call->timer); 6088c3e34a4SDavid Howells 609a641fd00SDavid Howells rxrpc_cleanup_ring(call); 610a4ea4c47SDavid Howells while ((txb = list_first_entry_or_null(&call->tx_buffer, 611a4ea4c47SDavid Howells struct rxrpc_txbuf, call_link))) { 612a4ea4c47SDavid Howells list_del(&txb->call_link); 613a4ea4c47SDavid Howells rxrpc_put_txbuf(txb, rxrpc_txbuf_put_cleaned); 614a4ea4c47SDavid Howells } 615a4ea4c47SDavid Howells rxrpc_put_txbuf(call->tx_pending, rxrpc_txbuf_put_cleaned); 6169a36a6bcSDavid Howells rxrpc_free_skb(call->acks_soft_tbl, rxrpc_skb_put_ack); 617*3feda9d6SDavid Howells rxrpc_put_connection(call->conn, rxrpc_conn_put_call); 618*3feda9d6SDavid Howells rxrpc_put_peer(call->peer, rxrpc_peer_put_call); 619*3feda9d6SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_free_call); 620*3feda9d6SDavid Howells } 6218c3e34a4SDavid Howells 622*3feda9d6SDavid Howells /* 623*3feda9d6SDavid Howells * clean up a call 624*3feda9d6SDavid Howells */ 625*3feda9d6SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 626*3feda9d6SDavid Howells { 627*3feda9d6SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 628*3feda9d6SDavid Howells 629*3feda9d6SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 630*3feda9d6SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 631*3feda9d6SDavid Howells 632*3feda9d6SDavid Howells del_timer_sync(&call->timer); 633*3feda9d6SDavid Howells cancel_work(&call->processor); 634*3feda9d6SDavid Howells 635*3feda9d6SDavid Howells if (in_softirq() || work_busy(&call->processor)) 636*3feda9d6SDavid Howells /* Can't use the rxrpc workqueue as we need to cancel/flush 637*3feda9d6SDavid Howells * something that may be running/waiting there. 638*3feda9d6SDavid Howells */ 639*3feda9d6SDavid Howells schedule_work(&call->destroyer); 640*3feda9d6SDavid Howells else 641*3feda9d6SDavid Howells rxrpc_destroy_call(&call->destroyer); 6428c3e34a4SDavid Howells } 6438c3e34a4SDavid Howells 6448c3e34a4SDavid Howells /* 6452baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 6462baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 6472baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 6488c3e34a4SDavid Howells */ 6492baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 6508c3e34a4SDavid Howells { 6518c3e34a4SDavid Howells struct rxrpc_call *call; 6528c3e34a4SDavid Howells 6538c3e34a4SDavid Howells _enter(""); 6548d94aa38SDavid Howells 655b1302342SDavid Howells if (!list_empty(&rxnet->calls)) { 656ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 6578c3e34a4SDavid Howells 6582baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 659b1302342SDavid Howells call = list_entry(rxnet->calls.next, 660b1302342SDavid Howells struct rxrpc_call, link); 6618c3e34a4SDavid Howells _debug("Zapping call %p", call); 6628c3e34a4SDavid Howells 663cb0fc0c9SDavid Howells rxrpc_see_call(call, rxrpc_call_see_zap); 6648c3e34a4SDavid Howells list_del_init(&call->link); 6658c3e34a4SDavid Howells 666248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 667a0575429SDavid Howells call, refcount_read(&call->ref), 6688c3e34a4SDavid Howells rxrpc_call_states[call->state], 6698c3e34a4SDavid Howells call->flags, call->events); 6708c3e34a4SDavid Howells 671ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 6728c3e34a4SDavid Howells cond_resched(); 673ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 6748c3e34a4SDavid Howells } 6758c3e34a4SDavid Howells 676ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 677b1302342SDavid Howells } 678d3be4d24SDavid Howells 679d3be4d24SDavid Howells atomic_dec(&rxnet->nr_calls); 6805bb053beSLinus Torvalds wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls)); 6818c3e34a4SDavid Howells } 682