12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 28c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling 38c3e34a4SDavid Howells * 48c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 58c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 68c3e34a4SDavid Howells */ 78c3e34a4SDavid Howells 88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c3e34a4SDavid Howells 108c3e34a4SDavid Howells #include <linux/slab.h> 118c3e34a4SDavid Howells #include <linux/module.h> 128c3e34a4SDavid Howells #include <linux/circ_buf.h> 138c3e34a4SDavid Howells #include <linux/spinlock_types.h> 148c3e34a4SDavid Howells #include <net/sock.h> 158c3e34a4SDavid Howells #include <net/af_rxrpc.h> 168c3e34a4SDavid Howells #include "ar-internal.h" 178c3e34a4SDavid Howells 188c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = { 19999b69f8SDavid Howells [RXRPC_CALL_UNINITIALISED] = "Uninit ", 20999b69f8SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn", 218c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", 228c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", 238c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", 2400e90712SDavid Howells [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc", 258c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SECURING] = "SvSecure", 268c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept", 278c3e34a4SDavid Howells [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", 288c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", 298c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", 308c3e34a4SDavid Howells [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", 318c3e34a4SDavid Howells [RXRPC_CALL_COMPLETE] = "Complete", 32f5c17aaeSDavid Howells }; 33f5c17aaeSDavid Howells 34f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = { 35f5c17aaeSDavid Howells [RXRPC_CALL_SUCCEEDED] = "Complete", 368c3e34a4SDavid Howells [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", 378c3e34a4SDavid Howells [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", 38f5c17aaeSDavid Howells [RXRPC_CALL_LOCAL_ERROR] = "LocError", 398c3e34a4SDavid Howells [RXRPC_CALL_NETWORK_ERROR] = "NetError", 408c3e34a4SDavid Howells }; 418c3e34a4SDavid Howells 428c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar; 438c3e34a4SDavid Howells 44b7a7d674SDavid Howells static struct semaphore rxrpc_call_limiter = 45b7a7d674SDavid Howells __SEMAPHORE_INITIALIZER(rxrpc_call_limiter, 1000); 46b7a7d674SDavid Howells static struct semaphore rxrpc_kernel_call_limiter = 47b7a7d674SDavid Howells __SEMAPHORE_INITIALIZER(rxrpc_kernel_call_limiter, 1000); 48b7a7d674SDavid Howells 49e99e88a9SKees Cook static void rxrpc_call_timer_expired(struct timer_list *t) 50248f219cSDavid Howells { 51e99e88a9SKees Cook struct rxrpc_call *call = from_timer(call, t, timer); 52248f219cSDavid Howells 53248f219cSDavid Howells _enter("%d", call->debug_id); 54248f219cSDavid Howells 55a158bdd3SDavid Howells if (call->state < RXRPC_CALL_COMPLETE) { 56a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies); 57a158bdd3SDavid Howells rxrpc_queue_call(call); 58fc7ab6d2SDavid Howells } 59248f219cSDavid Howells } 608c3e34a4SDavid Howells 619faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 628c3e34a4SDavid Howells 638c3e34a4SDavid Howells /* 648c3e34a4SDavid Howells * find an extant server call 658c3e34a4SDavid Howells * - called in process context with IRQs enabled 668c3e34a4SDavid Howells */ 678c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 688c3e34a4SDavid Howells unsigned long user_call_ID) 698c3e34a4SDavid Howells { 708c3e34a4SDavid Howells struct rxrpc_call *call; 718c3e34a4SDavid Howells struct rb_node *p; 728c3e34a4SDavid Howells 738c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 748c3e34a4SDavid Howells 758c3e34a4SDavid Howells read_lock(&rx->call_lock); 768c3e34a4SDavid Howells 778c3e34a4SDavid Howells p = rx->calls.rb_node; 788c3e34a4SDavid Howells while (p) { 798c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 808c3e34a4SDavid Howells 818c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 828c3e34a4SDavid Howells p = p->rb_left; 838c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 848c3e34a4SDavid Howells p = p->rb_right; 858c3e34a4SDavid Howells else 868c3e34a4SDavid Howells goto found_extant_call; 878c3e34a4SDavid Howells } 888c3e34a4SDavid Howells 898c3e34a4SDavid Howells read_unlock(&rx->call_lock); 908c3e34a4SDavid Howells _leave(" = NULL"); 918c3e34a4SDavid Howells return NULL; 928c3e34a4SDavid Howells 938c3e34a4SDavid Howells found_extant_call: 94fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 958c3e34a4SDavid Howells read_unlock(&rx->call_lock); 968c3e34a4SDavid Howells _leave(" = %p [%d]", call, atomic_read(&call->usage)); 978c3e34a4SDavid Howells return call; 988c3e34a4SDavid Howells } 998c3e34a4SDavid Howells 1008c3e34a4SDavid Howells /* 1018c3e34a4SDavid Howells * allocate a new call 1028c3e34a4SDavid Howells */ 103a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp, 104a25e21f0SDavid Howells unsigned int debug_id) 1058c3e34a4SDavid Howells { 1068c3e34a4SDavid Howells struct rxrpc_call *call; 107d3be4d24SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 1088c3e34a4SDavid Howells 1098c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1108c3e34a4SDavid Howells if (!call) 1118c3e34a4SDavid Howells return NULL; 1128c3e34a4SDavid Howells 113248f219cSDavid Howells call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE, 114248f219cSDavid Howells sizeof(struct sk_buff *), 1158c3e34a4SDavid Howells gfp); 116248f219cSDavid Howells if (!call->rxtx_buffer) 117248f219cSDavid Howells goto nomem; 1188c3e34a4SDavid Howells 119248f219cSDavid Howells call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp); 120248f219cSDavid Howells if (!call->rxtx_annotations) 121248f219cSDavid Howells goto nomem_2; 122248f219cSDavid 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); 1338c3e34a4SDavid Howells INIT_WORK(&call->processor, &rxrpc_process_call); 134999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 13545025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1368c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 137248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 138248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 13945025bceSDavid Howells init_waitqueue_head(&call->waitq); 1408c3e34a4SDavid Howells spin_lock_init(&call->lock); 14120acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 142c1e15b49SDavid Howells spin_lock_init(&call->input_lock); 1438c3e34a4SDavid Howells rwlock_init(&call->state_lock); 1448c3e34a4SDavid Howells atomic_set(&call->usage, 1); 145a25e21f0SDavid Howells call->debug_id = debug_id; 146e754eba6SDavid Howells call->tx_total_len = -1; 147a158bdd3SDavid Howells call->next_rx_timo = 20 * HZ; 148a158bdd3SDavid Howells call->next_req_timo = 1 * HZ; 1498c3e34a4SDavid Howells 1508c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1518c3e34a4SDavid Howells 152248f219cSDavid Howells /* Leave space in the ring to handle a maxed-out jumbo packet */ 15375e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 154248f219cSDavid Howells call->tx_winsize = 16; 155248f219cSDavid Howells call->rx_expect_next = 1; 15657494343SDavid Howells 15757494343SDavid Howells call->cong_cwnd = 2; 15857494343SDavid Howells call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1; 159d3be4d24SDavid Howells 160d3be4d24SDavid Howells call->rxnet = rxnet; 1614700c4d8SDavid Howells call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK; 162d3be4d24SDavid Howells atomic_inc(&rxnet->nr_calls); 1638c3e34a4SDavid Howells return call; 164248f219cSDavid Howells 165248f219cSDavid Howells nomem_2: 166248f219cSDavid Howells kfree(call->rxtx_buffer); 167248f219cSDavid Howells nomem: 168248f219cSDavid Howells kmem_cache_free(rxrpc_call_jar, call); 169248f219cSDavid Howells return NULL; 1708c3e34a4SDavid Howells } 1718c3e34a4SDavid Howells 1728c3e34a4SDavid Howells /* 173999b69f8SDavid Howells * Allocate a new client call. 1748c3e34a4SDavid Howells */ 1759faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 1769faaff59SDavid Howells struct sockaddr_rxrpc *srx, 177a25e21f0SDavid Howells gfp_t gfp, 178a25e21f0SDavid Howells unsigned int debug_id) 1798c3e34a4SDavid Howells { 1808c3e34a4SDavid Howells struct rxrpc_call *call; 18157494343SDavid Howells ktime_t now; 1828c3e34a4SDavid Howells 1838c3e34a4SDavid Howells _enter(""); 1848c3e34a4SDavid Howells 185a25e21f0SDavid Howells call = rxrpc_alloc_call(rx, gfp, debug_id); 1868c3e34a4SDavid Howells if (!call) 1878c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 188999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 189999b69f8SDavid Howells call->service_id = srx->srx_service; 19071f3ca40SDavid Howells call->tx_phase = true; 19157494343SDavid Howells now = ktime_get_real(); 19257494343SDavid Howells call->acks_latest_ts = now; 19357494343SDavid Howells call->cong_tstamp = now; 194999b69f8SDavid Howells 195999b69f8SDavid Howells _leave(" = %p", call); 196999b69f8SDavid Howells return call; 197999b69f8SDavid Howells } 198999b69f8SDavid Howells 199999b69f8SDavid Howells /* 200248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 201999b69f8SDavid Howells */ 202248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 203999b69f8SDavid Howells { 204a158bdd3SDavid Howells unsigned long now = jiffies; 205a158bdd3SDavid Howells unsigned long j = now + MAX_JIFFY_OFFSET; 206999b69f8SDavid Howells 207a158bdd3SDavid Howells call->ack_at = j; 208bd1fdf8cSDavid Howells call->ack_lost_at = j; 209a158bdd3SDavid Howells call->resend_at = j; 210a158bdd3SDavid Howells call->ping_at = j; 211a158bdd3SDavid Howells call->expect_rx_by = j; 212a158bdd3SDavid Howells call->expect_req_by = j; 213a158bdd3SDavid Howells call->expect_term_by = j; 214a158bdd3SDavid Howells call->timer.expires = now; 2158c3e34a4SDavid Howells } 2168c3e34a4SDavid Howells 2178c3e34a4SDavid Howells /* 218b7a7d674SDavid Howells * Wait for a call slot to become available. 219b7a7d674SDavid Howells */ 220b7a7d674SDavid Howells static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp) 221b7a7d674SDavid Howells { 222b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 223b7a7d674SDavid Howells 224b7a7d674SDavid Howells if (p->kernel) 225b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 226b7a7d674SDavid Howells if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) { 227b7a7d674SDavid Howells down(limiter); 228b7a7d674SDavid Howells return limiter; 229b7a7d674SDavid Howells } 230b7a7d674SDavid Howells return down_interruptible(limiter) < 0 ? NULL : limiter; 231b7a7d674SDavid Howells } 232b7a7d674SDavid Howells 233b7a7d674SDavid Howells /* 234b7a7d674SDavid Howells * Release a call slot. 235b7a7d674SDavid Howells */ 236b7a7d674SDavid Howells static void rxrpc_put_call_slot(struct rxrpc_call *call) 237b7a7d674SDavid Howells { 238b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 239b7a7d674SDavid Howells 240b7a7d674SDavid Howells if (test_bit(RXRPC_CALL_KERNEL, &call->flags)) 241b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 242b7a7d674SDavid Howells up(limiter); 243b7a7d674SDavid Howells } 244b7a7d674SDavid Howells 245b7a7d674SDavid Howells /* 246540b1c48SDavid Howells * Set up a call for the given parameters. 247540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 248540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2498c3e34a4SDavid Howells */ 2508c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 25119ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 252999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 25348124178SDavid Howells struct rxrpc_call_params *p, 254a25e21f0SDavid Howells gfp_t gfp, 255a25e21f0SDavid Howells unsigned int debug_id) 256540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 25788f2a825SDavid Howells __acquires(&call->user_mutex) 2588c3e34a4SDavid Howells { 2598c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 260d3be4d24SDavid Howells struct rxrpc_net *rxnet; 261b7a7d674SDavid Howells struct semaphore *limiter; 2628c3e34a4SDavid Howells struct rb_node *parent, **pp; 263e34d4234SDavid Howells const void *here = __builtin_return_address(0); 264999b69f8SDavid Howells int ret; 2658c3e34a4SDavid Howells 26648124178SDavid Howells _enter("%p,%lx", rx, p->user_call_ID); 2678c3e34a4SDavid Howells 268b7a7d674SDavid Howells limiter = rxrpc_get_call_slot(p, gfp); 269b7a7d674SDavid Howells if (!limiter) 270b7a7d674SDavid Howells return ERR_PTR(-ERESTARTSYS); 271b7a7d674SDavid Howells 272a25e21f0SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id); 2738c3e34a4SDavid Howells if (IS_ERR(call)) { 274540b1c48SDavid Howells release_sock(&rx->sk); 275b7a7d674SDavid Howells up(limiter); 2768c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 2778c3e34a4SDavid Howells return call; 2788c3e34a4SDavid Howells } 2798c3e34a4SDavid Howells 280e138aa7dSDavid Howells call->interruptibility = p->interruptibility; 28148124178SDavid Howells call->tx_total_len = p->tx_total_len; 28248c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_new_client, 28348c9e0ecSDavid Howells atomic_read(&call->usage), 28448124178SDavid Howells here, (const void *)p->user_call_ID); 285b7a7d674SDavid Howells if (p->kernel) 286b7a7d674SDavid Howells __set_bit(RXRPC_CALL_KERNEL, &call->flags); 287e34d4234SDavid Howells 288540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 289540b1c48SDavid Howells * will be acting outside the socket lock. 290540b1c48SDavid Howells */ 291540b1c48SDavid Howells mutex_lock(&call->user_mutex); 292540b1c48SDavid Howells 293999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 2948c3e34a4SDavid Howells write_lock(&rx->call_lock); 2958c3e34a4SDavid Howells 2968c3e34a4SDavid Howells pp = &rx->calls.rb_node; 2978c3e34a4SDavid Howells parent = NULL; 2988c3e34a4SDavid Howells while (*pp) { 2998c3e34a4SDavid Howells parent = *pp; 3008c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 3018c3e34a4SDavid Howells 30248124178SDavid Howells if (p->user_call_ID < xcall->user_call_ID) 3038c3e34a4SDavid Howells pp = &(*pp)->rb_left; 30448124178SDavid Howells else if (p->user_call_ID > xcall->user_call_ID) 3058c3e34a4SDavid Howells pp = &(*pp)->rb_right; 3068c3e34a4SDavid Howells else 307357f5ef6SDavid Howells goto error_dup_user_ID; 3088c3e34a4SDavid Howells } 3098c3e34a4SDavid Howells 310248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 31148124178SDavid Howells call->user_call_ID = p->user_call_ID; 312357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 313fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got_userid); 3148c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 3158c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 316248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 317248f219cSDavid Howells 3188c3e34a4SDavid Howells write_unlock(&rx->call_lock); 3198c3e34a4SDavid Howells 320d3be4d24SDavid Howells rxnet = call->rxnet; 3212baec2c3SDavid Howells write_lock(&rxnet->call_lock); 3222baec2c3SDavid Howells list_add_tail(&call->link, &rxnet->calls); 3232baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 3248c3e34a4SDavid Howells 325540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 326540b1c48SDavid Howells release_sock(&rx->sk); 327540b1c48SDavid Howells 328248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 329248f219cSDavid Howells * including channel number and call ID. 330248f219cSDavid Howells */ 3315e33a23bSDavid Howells ret = rxrpc_connect_call(rx, call, cp, srx, gfp); 332999b69f8SDavid Howells if (ret < 0) 33365550098SDavid Howells goto error_attached_to_socket; 334999b69f8SDavid Howells 33548c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_connected, 33648c9e0ecSDavid Howells atomic_read(&call->usage), here, NULL); 337a84a46d7SDavid Howells 338248f219cSDavid Howells rxrpc_start_call_timer(call); 339248f219cSDavid Howells 3408c3e34a4SDavid Howells _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); 3418c3e34a4SDavid Howells 3428c3e34a4SDavid Howells _leave(" = %p [new]", call); 3438c3e34a4SDavid Howells return call; 3448c3e34a4SDavid Howells 3458c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 3468c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 3478c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 3488c3e34a4SDavid Howells * same time in different threads. 3498c3e34a4SDavid Howells */ 350357f5ef6SDavid Howells error_dup_user_ID: 3518c3e34a4SDavid Howells write_unlock(&rx->call_lock); 352540b1c48SDavid Howells release_sock(&rx->sk); 353357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 35465550098SDavid Howells RX_CALL_DEAD, -EEXIST); 35548c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_error, 35665550098SDavid Howells atomic_read(&call->usage), here, ERR_PTR(-EEXIST)); 357357f5ef6SDavid Howells rxrpc_release_call(rx, call); 358540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 359357f5ef6SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 36065550098SDavid Howells _leave(" = -EEXIST"); 36165550098SDavid Howells return ERR_PTR(-EEXIST); 36265550098SDavid Howells 36365550098SDavid Howells /* We got an error, but the call is attached to the socket and is in 36465550098SDavid Howells * need of release. However, we might now race with recvmsg() when 36565550098SDavid Howells * completing the call queues it. Return 0 from sys_sendmsg() and 36665550098SDavid Howells * leave the error to recvmsg() to deal with. 36765550098SDavid Howells */ 36865550098SDavid Howells error_attached_to_socket: 36965550098SDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_error, 37065550098SDavid Howells atomic_read(&call->usage), here, ERR_PTR(ret)); 37165550098SDavid Howells set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); 37265550098SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 37365550098SDavid Howells RX_CALL_DEAD, ret); 37465550098SDavid Howells _leave(" = c=%08x [err]", call->debug_id); 37565550098SDavid Howells return call; 3768c3e34a4SDavid Howells } 3778c3e34a4SDavid Howells 3788c3e34a4SDavid Howells /* 379248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 380248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 3818c3e34a4SDavid Howells */ 382248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 383248f219cSDavid Howells struct rxrpc_call *call, 38442886ffeSDavid Howells struct sk_buff *skb) 3858c3e34a4SDavid Howells { 386248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 38742886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 388248f219cSDavid Howells u32 chan; 3898c3e34a4SDavid Howells 390248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 3918c3e34a4SDavid Howells 392248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 393248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 394248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 395248f219cSDavid Howells call->cid = sp->hdr.cid; 396248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_ACCEPTING; 397248f219cSDavid Howells if (sp->hdr.securityIndex > 0) 398248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 39957494343SDavid Howells call->cong_tstamp = skb->tstamp; 4008c3e34a4SDavid Howells 401248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 402248f219cSDavid Howells * only defending against the data_ready handler (which we're called 403248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 404248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 405248f219cSDavid Howells * call pointer). 4068c3e34a4SDavid Howells */ 407248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 408248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 409248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 410a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 4118c3e34a4SDavid Howells 41285f32278SDavid Howells spin_lock(&conn->params.peer->lock); 413f3344303SDavid Howells hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets); 41485f32278SDavid Howells spin_unlock(&conn->params.peer->lock); 4158c3e34a4SDavid Howells 4168c3e34a4SDavid Howells _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); 4178c3e34a4SDavid Howells 418248f219cSDavid Howells rxrpc_start_call_timer(call); 419248f219cSDavid Howells _leave(""); 4208c3e34a4SDavid Howells } 4218c3e34a4SDavid Howells 4228c3e34a4SDavid Howells /* 4238d94aa38SDavid Howells * Queue a call's work processor, getting a ref to pass to the work queue. 4248d94aa38SDavid Howells */ 4258d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call) 4268d94aa38SDavid Howells { 4278d94aa38SDavid Howells const void *here = __builtin_return_address(0); 428bfc18e38SMark Rutland int n = atomic_fetch_add_unless(&call->usage, 1, 0); 4298d94aa38SDavid Howells if (n == 0) 4308d94aa38SDavid Howells return false; 4318d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 43248c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1, 43348c9e0ecSDavid Howells here, NULL); 4348d94aa38SDavid Howells else 4358d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4368d94aa38SDavid Howells return true; 4378d94aa38SDavid Howells } 4388d94aa38SDavid Howells 4398d94aa38SDavid Howells /* 4408d94aa38SDavid Howells * Queue a call's work processor, passing the callers ref to the work queue. 4418d94aa38SDavid Howells */ 4428d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call) 4438d94aa38SDavid Howells { 4448d94aa38SDavid Howells const void *here = __builtin_return_address(0); 4458d94aa38SDavid Howells int n = atomic_read(&call->usage); 4468d94aa38SDavid Howells ASSERTCMP(n, >=, 1); 4478d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 44848c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n, 44948c9e0ecSDavid Howells here, NULL); 4508d94aa38SDavid Howells else 4518d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4528d94aa38SDavid Howells return true; 4538d94aa38SDavid Howells } 4548d94aa38SDavid Howells 4558d94aa38SDavid Howells /* 456e34d4234SDavid Howells * Note the re-emergence of a call. 457e34d4234SDavid Howells */ 458e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call) 459e34d4234SDavid Howells { 460e34d4234SDavid Howells const void *here = __builtin_return_address(0); 461e34d4234SDavid Howells if (call) { 462e34d4234SDavid Howells int n = atomic_read(&call->usage); 463e34d4234SDavid Howells 46448c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n, 46548c9e0ecSDavid Howells here, NULL); 466e34d4234SDavid Howells } 467e34d4234SDavid Howells } 468e34d4234SDavid Howells 469e34d4234SDavid Howells /* 470e34d4234SDavid Howells * Note the addition of a ref on a call. 471e34d4234SDavid Howells */ 472fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 473e34d4234SDavid Howells { 474e34d4234SDavid Howells const void *here = __builtin_return_address(0); 475e34d4234SDavid Howells int n = atomic_inc_return(&call->usage); 476e34d4234SDavid Howells 47748c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, op, n, here, NULL); 478e34d4234SDavid Howells } 479e34d4234SDavid Howells 480e34d4234SDavid Howells /* 481a641fd00SDavid Howells * Clean up the RxTx skb ring. 482a641fd00SDavid Howells */ 483a641fd00SDavid Howells static void rxrpc_cleanup_ring(struct rxrpc_call *call) 484a641fd00SDavid Howells { 485a641fd00SDavid Howells int i; 486a641fd00SDavid Howells 487a641fd00SDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { 488987db9f7SDavid Howells rxrpc_free_skb(call->rxtx_buffer[i], rxrpc_skb_cleaned); 489a641fd00SDavid Howells call->rxtx_buffer[i] = NULL; 490a641fd00SDavid Howells } 491a641fd00SDavid Howells } 492a641fd00SDavid Howells 493a641fd00SDavid Howells /* 494248f219cSDavid Howells * Detach a call from its owning socket. 4958c3e34a4SDavid Howells */ 4968d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 4978c3e34a4SDavid Howells { 498a84a46d7SDavid Howells const void *here = __builtin_return_address(0); 499248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 500248f219cSDavid Howells bool put = false; 501248f219cSDavid Howells 502248f219cSDavid Howells _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage)); 503248f219cSDavid Howells 50448c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_release, 50548c9e0ecSDavid Howells atomic_read(&call->usage), 506a84a46d7SDavid Howells here, (const void *)call->flags); 5078c3e34a4SDavid Howells 508a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 509e34d4234SDavid Howells 5108c3e34a4SDavid Howells spin_lock_bh(&call->lock); 5118c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 5128c3e34a4SDavid Howells BUG(); 5138c3e34a4SDavid Howells spin_unlock_bh(&call->lock); 5148c3e34a4SDavid Howells 515b7a7d674SDavid Howells rxrpc_put_call_slot(call); 516b7a7d674SDavid Howells 517248f219cSDavid Howells del_timer_sync(&call->timer); 5188c3e34a4SDavid Howells 519248f219cSDavid Howells /* Make sure we don't get any more notifications */ 520248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 521e653cfe4SDavid Howells 522248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 5238c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 5248c3e34a4SDavid Howells call, call->events, call->flags); 525248f219cSDavid Howells list_del(&call->recvmsg_link); 526248f219cSDavid Howells put = true; 527248f219cSDavid Howells } 528248f219cSDavid Howells 529248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 530248f219cSDavid Howells call->recvmsg_link.next = NULL; 531248f219cSDavid Howells call->recvmsg_link.prev = NULL; 532248f219cSDavid Howells 533248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 534248f219cSDavid Howells if (put) 535248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 536248f219cSDavid Howells 537248f219cSDavid Howells write_lock(&rx->call_lock); 538248f219cSDavid Howells 539248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 5408c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 5418c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 5428d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid); 5438c3e34a4SDavid Howells } 5448c3e34a4SDavid Howells 545248f219cSDavid Howells list_del(&call->sock_link); 546248f219cSDavid Howells write_unlock(&rx->call_lock); 5478c3e34a4SDavid Howells 548248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 5498c3e34a4SDavid Howells 5505273a191SDavid Howells if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 551e653cfe4SDavid Howells rxrpc_disconnect_call(call); 55291fcfbe8SDavid Howells if (call->security) 55391fcfbe8SDavid Howells call->security->free_call_crypto(call); 554e653cfe4SDavid Howells 555a641fd00SDavid Howells rxrpc_cleanup_ring(call); 5568c3e34a4SDavid Howells _leave(""); 5578c3e34a4SDavid Howells } 5588c3e34a4SDavid Howells 5598c3e34a4SDavid Howells /* 5608c3e34a4SDavid Howells * release all the calls associated with a socket 5618c3e34a4SDavid Howells */ 5628c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 5638c3e34a4SDavid Howells { 5648c3e34a4SDavid Howells struct rxrpc_call *call; 5658c3e34a4SDavid Howells 5668c3e34a4SDavid Howells _enter("%p", rx); 5678c3e34a4SDavid Howells 5680360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 5690360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 5700360da6dSDavid Howells struct rxrpc_call, accept_link); 5710360da6dSDavid Howells list_del(&call->accept_link); 5723a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 5730360da6dSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5740360da6dSDavid Howells } 5750360da6dSDavid Howells 576248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 577248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 578248f219cSDavid Howells struct rxrpc_call, sock_link); 579248f219cSDavid Howells rxrpc_get_call(call, rxrpc_call_got); 5803a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 58126cb02aaSDavid Howells rxrpc_send_abort_packet(call); 5828d94aa38SDavid Howells rxrpc_release_call(rx, call); 583248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5848c3e34a4SDavid Howells } 5858c3e34a4SDavid Howells 5868c3e34a4SDavid Howells _leave(""); 5878c3e34a4SDavid Howells } 5888c3e34a4SDavid Howells 5898c3e34a4SDavid Howells /* 5908c3e34a4SDavid Howells * release a call 5918c3e34a4SDavid Howells */ 592fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 5938c3e34a4SDavid Howells { 594d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 595e34d4234SDavid Howells const void *here = __builtin_return_address(0); 59648c9e0ecSDavid Howells unsigned int debug_id = call->debug_id; 5972ab27215SDavid Howells int n; 598e34d4234SDavid Howells 5998c3e34a4SDavid Howells ASSERT(call != NULL); 6008c3e34a4SDavid Howells 601e34d4234SDavid Howells n = atomic_dec_return(&call->usage); 60248c9e0ecSDavid Howells trace_rxrpc_call(debug_id, op, n, here, NULL); 603e34d4234SDavid Howells ASSERTCMP(n, >=, 0); 604e34d4234SDavid Howells if (n == 0) { 6058c3e34a4SDavid Howells _debug("call %d dead", call->debug_id); 606248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 607e34d4234SDavid Howells 6082baec2c3SDavid Howells if (!list_empty(&call->link)) { 6092baec2c3SDavid Howells write_lock(&rxnet->call_lock); 610248f219cSDavid Howells list_del_init(&call->link); 6112baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 6122baec2c3SDavid Howells } 613e34d4234SDavid Howells 6148d94aa38SDavid Howells rxrpc_cleanup_call(call); 615e34d4234SDavid Howells } 6168c3e34a4SDavid Howells } 6178c3e34a4SDavid Howells 6188c3e34a4SDavid Howells /* 619963485d4SDavid Howells * Final call destruction - but must be done in process context. 620dee46364SDavid Howells */ 621963485d4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work) 622dee46364SDavid Howells { 623963485d4SDavid Howells struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor); 624d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 625dee46364SDavid Howells 6265273a191SDavid Howells rxrpc_put_connection(call->conn); 627df5d8bf7SDavid Howells rxrpc_put_peer(call->peer); 628248f219cSDavid Howells kfree(call->rxtx_buffer); 629248f219cSDavid Howells kfree(call->rxtx_annotations); 630dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 631d3be4d24SDavid Howells if (atomic_dec_and_test(&rxnet->nr_calls)) 6325bb053beSLinus Torvalds wake_up_var(&rxnet->nr_calls); 633dee46364SDavid Howells } 634dee46364SDavid Howells 635dee46364SDavid Howells /* 636963485d4SDavid Howells * Final call destruction under RCU. 637963485d4SDavid Howells */ 638963485d4SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu) 639963485d4SDavid Howells { 640963485d4SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 641963485d4SDavid Howells 642963485d4SDavid Howells if (in_softirq()) { 643963485d4SDavid Howells INIT_WORK(&call->processor, rxrpc_destroy_call); 644963485d4SDavid Howells if (!rxrpc_queue_work(&call->processor)) 645963485d4SDavid Howells BUG(); 646963485d4SDavid Howells } else { 647963485d4SDavid Howells rxrpc_destroy_call(&call->processor); 648963485d4SDavid Howells } 649963485d4SDavid Howells } 650963485d4SDavid Howells 651963485d4SDavid Howells /* 6528c3e34a4SDavid Howells * clean up a call 6538c3e34a4SDavid Howells */ 65400e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 6558c3e34a4SDavid Howells { 656248f219cSDavid Howells _net("DESTROY CALL %d", call->debug_id); 6578c3e34a4SDavid Howells 6588c3e34a4SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 6598c3e34a4SDavid Howells 660248f219cSDavid Howells del_timer_sync(&call->timer); 6618c3e34a4SDavid Howells 6628d94aa38SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 6638c3e34a4SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 6648c3e34a4SDavid Howells 665a641fd00SDavid Howells rxrpc_cleanup_ring(call); 666987db9f7SDavid Howells rxrpc_free_skb(call->tx_pending, rxrpc_skb_cleaned); 6678c3e34a4SDavid Howells 668dee46364SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_destroy_call); 6698c3e34a4SDavid Howells } 6708c3e34a4SDavid Howells 6718c3e34a4SDavid Howells /* 6722baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 6732baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 6742baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 6758c3e34a4SDavid Howells */ 6762baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 6778c3e34a4SDavid Howells { 6788c3e34a4SDavid Howells struct rxrpc_call *call; 6798c3e34a4SDavid Howells 6808c3e34a4SDavid Howells _enter(""); 6818d94aa38SDavid Howells 682b1302342SDavid Howells if (!list_empty(&rxnet->calls)) { 6832baec2c3SDavid Howells write_lock(&rxnet->call_lock); 6848c3e34a4SDavid Howells 6852baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 686b1302342SDavid Howells call = list_entry(rxnet->calls.next, 687b1302342SDavid Howells struct rxrpc_call, link); 6888c3e34a4SDavid Howells _debug("Zapping call %p", call); 6898c3e34a4SDavid Howells 690e34d4234SDavid Howells rxrpc_see_call(call); 6918c3e34a4SDavid Howells list_del_init(&call->link); 6928c3e34a4SDavid Howells 693248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 6948c3e34a4SDavid Howells call, atomic_read(&call->usage), 6958c3e34a4SDavid Howells rxrpc_call_states[call->state], 6968c3e34a4SDavid Howells call->flags, call->events); 6978c3e34a4SDavid Howells 6982baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 6998c3e34a4SDavid Howells cond_resched(); 7002baec2c3SDavid Howells write_lock(&rxnet->call_lock); 7018c3e34a4SDavid Howells } 7028c3e34a4SDavid Howells 7032baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 704b1302342SDavid Howells } 705d3be4d24SDavid Howells 706d3be4d24SDavid Howells atomic_dec(&rxnet->nr_calls); 7075bb053beSLinus Torvalds wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls)); 7088c3e34a4SDavid Howells } 709