12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 28c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling 38c3e34a4SDavid Howells * 48c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 58c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 68c3e34a4SDavid Howells */ 78c3e34a4SDavid Howells 88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c3e34a4SDavid Howells 108c3e34a4SDavid Howells #include <linux/slab.h> 118c3e34a4SDavid Howells #include <linux/module.h> 128c3e34a4SDavid Howells #include <linux/circ_buf.h> 138c3e34a4SDavid Howells #include <linux/spinlock_types.h> 148c3e34a4SDavid Howells #include <net/sock.h> 158c3e34a4SDavid Howells #include <net/af_rxrpc.h> 168c3e34a4SDavid Howells #include "ar-internal.h" 178c3e34a4SDavid Howells 188c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = { 19999b69f8SDavid Howells [RXRPC_CALL_UNINITIALISED] = "Uninit ", 20999b69f8SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn", 218c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", 228c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", 238c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", 2400e90712SDavid Howells [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc", 258c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SECURING] = "SvSecure", 268c3e34a4SDavid Howells [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", 278c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", 288c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", 298c3e34a4SDavid Howells [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", 308c3e34a4SDavid Howells [RXRPC_CALL_COMPLETE] = "Complete", 31f5c17aaeSDavid Howells }; 32f5c17aaeSDavid Howells 33f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = { 34f5c17aaeSDavid Howells [RXRPC_CALL_SUCCEEDED] = "Complete", 358c3e34a4SDavid Howells [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", 368c3e34a4SDavid Howells [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", 37f5c17aaeSDavid Howells [RXRPC_CALL_LOCAL_ERROR] = "LocError", 388c3e34a4SDavid Howells [RXRPC_CALL_NETWORK_ERROR] = "NetError", 398c3e34a4SDavid Howells }; 408c3e34a4SDavid Howells 418c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar; 428c3e34a4SDavid Howells 43b7a7d674SDavid Howells static struct semaphore rxrpc_call_limiter = 44b7a7d674SDavid Howells __SEMAPHORE_INITIALIZER(rxrpc_call_limiter, 1000); 45b7a7d674SDavid Howells static struct semaphore rxrpc_kernel_call_limiter = 46b7a7d674SDavid Howells __SEMAPHORE_INITIALIZER(rxrpc_kernel_call_limiter, 1000); 47b7a7d674SDavid Howells 48e99e88a9SKees Cook static void rxrpc_call_timer_expired(struct timer_list *t) 49248f219cSDavid Howells { 50e99e88a9SKees Cook struct rxrpc_call *call = from_timer(call, t, timer); 51248f219cSDavid Howells 52248f219cSDavid Howells _enter("%d", call->debug_id); 53248f219cSDavid Howells 54a158bdd3SDavid Howells if (call->state < RXRPC_CALL_COMPLETE) { 55334dfbfcSDavid Howells trace_rxrpc_timer_expired(call, jiffies); 564a7f62f9SDavid Howells __rxrpc_queue_call(call); 574a7f62f9SDavid Howells } else { 584a7f62f9SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 59fc7ab6d2SDavid Howells } 60248f219cSDavid Howells } 618c3e34a4SDavid Howells 624a7f62f9SDavid Howells void rxrpc_reduce_call_timer(struct rxrpc_call *call, 634a7f62f9SDavid Howells unsigned long expire_at, 644a7f62f9SDavid Howells unsigned long now, 654a7f62f9SDavid Howells enum rxrpc_timer_trace why) 664a7f62f9SDavid Howells { 674a7f62f9SDavid Howells if (rxrpc_try_get_call(call, rxrpc_call_got_timer)) { 684a7f62f9SDavid Howells trace_rxrpc_timer(call, why, now); 694a7f62f9SDavid Howells if (timer_reduce(&call->timer, expire_at)) 704a7f62f9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_notimer); 714a7f62f9SDavid Howells } 724a7f62f9SDavid Howells } 734a7f62f9SDavid Howells 744a7f62f9SDavid Howells void rxrpc_delete_call_timer(struct rxrpc_call *call) 754a7f62f9SDavid Howells { 764a7f62f9SDavid Howells if (del_timer_sync(&call->timer)) 774a7f62f9SDavid Howells rxrpc_put_call(call, rxrpc_call_put_timer); 784a7f62f9SDavid Howells } 794a7f62f9SDavid Howells 809faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 818c3e34a4SDavid Howells 828c3e34a4SDavid Howells /* 838c3e34a4SDavid Howells * find an extant server call 848c3e34a4SDavid Howells * - called in process context with IRQs enabled 858c3e34a4SDavid Howells */ 868c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 878c3e34a4SDavid Howells unsigned long user_call_ID) 888c3e34a4SDavid Howells { 898c3e34a4SDavid Howells struct rxrpc_call *call; 908c3e34a4SDavid Howells struct rb_node *p; 918c3e34a4SDavid Howells 928c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 938c3e34a4SDavid Howells 948c3e34a4SDavid Howells read_lock(&rx->call_lock); 958c3e34a4SDavid Howells 968c3e34a4SDavid Howells p = rx->calls.rb_node; 978c3e34a4SDavid Howells while (p) { 988c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 998c3e34a4SDavid Howells 1008c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 1018c3e34a4SDavid Howells p = p->rb_left; 1028c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 1038c3e34a4SDavid Howells p = p->rb_right; 1048c3e34a4SDavid Howells else 1058c3e34a4SDavid Howells goto found_extant_call; 1068c3e34a4SDavid Howells } 1078c3e34a4SDavid Howells 1088c3e34a4SDavid Howells read_unlock(&rx->call_lock); 1098c3e34a4SDavid Howells _leave(" = NULL"); 1108c3e34a4SDavid Howells return NULL; 1118c3e34a4SDavid Howells 1128c3e34a4SDavid Howells found_extant_call: 113fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 1148c3e34a4SDavid Howells read_unlock(&rx->call_lock); 115a0575429SDavid Howells _leave(" = %p [%d]", call, refcount_read(&call->ref)); 1168c3e34a4SDavid Howells return call; 1178c3e34a4SDavid Howells } 1188c3e34a4SDavid Howells 1198c3e34a4SDavid Howells /* 1208c3e34a4SDavid Howells * allocate a new call 1218c3e34a4SDavid Howells */ 122a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp, 123a25e21f0SDavid Howells unsigned int debug_id) 1248c3e34a4SDavid Howells { 1258c3e34a4SDavid Howells struct rxrpc_call *call; 126d3be4d24SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 1278c3e34a4SDavid Howells 1288c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1298c3e34a4SDavid Howells if (!call) 1308c3e34a4SDavid Howells return NULL; 1318c3e34a4SDavid Howells 132248f219cSDavid Howells call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE, 133248f219cSDavid Howells sizeof(struct sk_buff *), 1348c3e34a4SDavid Howells gfp); 135248f219cSDavid Howells if (!call->rxtx_buffer) 136248f219cSDavid Howells goto nomem; 1378c3e34a4SDavid Howells 138248f219cSDavid Howells call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp); 139248f219cSDavid Howells if (!call->rxtx_annotations) 140248f219cSDavid Howells goto nomem_2; 141248f219cSDavid Howells 142540b1c48SDavid Howells mutex_init(&call->user_mutex); 1439faaff59SDavid Howells 1449faaff59SDavid Howells /* Prevent lockdep reporting a deadlock false positive between the afs 1459faaff59SDavid Howells * filesystem and sys_sendmsg() via the mmap sem. 1469faaff59SDavid Howells */ 1479faaff59SDavid Howells if (rx->sk.sk_kern_sock) 1489faaff59SDavid Howells lockdep_set_class(&call->user_mutex, 1499faaff59SDavid Howells &rxrpc_call_user_mutex_lock_class_key); 1509faaff59SDavid Howells 151e99e88a9SKees Cook timer_setup(&call->timer, rxrpc_call_timer_expired, 0); 1528c3e34a4SDavid Howells INIT_WORK(&call->processor, &rxrpc_process_call); 153999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 15445025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1558c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 156248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 157248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 158*5d7edbc9SDavid Howells skb_queue_head_init(&call->recvmsg_queue); 159*5d7edbc9SDavid Howells skb_queue_head_init(&call->rx_oos_queue); 16045025bceSDavid Howells init_waitqueue_head(&call->waitq); 1618c3e34a4SDavid Howells spin_lock_init(&call->lock); 16220acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 163c1e15b49SDavid Howells spin_lock_init(&call->input_lock); 1648c3e34a4SDavid Howells rwlock_init(&call->state_lock); 165a0575429SDavid Howells refcount_set(&call->ref, 1); 166a25e21f0SDavid Howells call->debug_id = debug_id; 167e754eba6SDavid Howells call->tx_total_len = -1; 168a158bdd3SDavid Howells call->next_rx_timo = 20 * HZ; 169a158bdd3SDavid Howells call->next_req_timo = 1 * HZ; 170*5d7edbc9SDavid Howells atomic64_set(&call->ackr_window, 0x100000001ULL); 1718c3e34a4SDavid Howells 1728c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1738c3e34a4SDavid Howells 17475e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 175248f219cSDavid Howells call->tx_winsize = 16; 17657494343SDavid Howells 17757494343SDavid Howells call->cong_cwnd = 2; 17857494343SDavid Howells call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1; 179d3be4d24SDavid Howells 180d3be4d24SDavid Howells call->rxnet = rxnet; 1814700c4d8SDavid Howells call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK; 182d3be4d24SDavid Howells atomic_inc(&rxnet->nr_calls); 1838c3e34a4SDavid Howells return call; 184248f219cSDavid Howells 185248f219cSDavid Howells nomem_2: 186248f219cSDavid Howells kfree(call->rxtx_buffer); 187248f219cSDavid Howells nomem: 188248f219cSDavid Howells kmem_cache_free(rxrpc_call_jar, call); 189248f219cSDavid Howells return NULL; 1908c3e34a4SDavid Howells } 1918c3e34a4SDavid Howells 1928c3e34a4SDavid Howells /* 193999b69f8SDavid Howells * Allocate a new client call. 1948c3e34a4SDavid Howells */ 1959faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 1969faaff59SDavid Howells struct sockaddr_rxrpc *srx, 197a25e21f0SDavid Howells gfp_t gfp, 198a25e21f0SDavid Howells unsigned int debug_id) 1998c3e34a4SDavid Howells { 2008c3e34a4SDavid Howells struct rxrpc_call *call; 20157494343SDavid Howells ktime_t now; 2028c3e34a4SDavid Howells 2038c3e34a4SDavid Howells _enter(""); 2048c3e34a4SDavid Howells 205a25e21f0SDavid Howells call = rxrpc_alloc_call(rx, gfp, debug_id); 2068c3e34a4SDavid Howells if (!call) 2078c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 208999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 209999b69f8SDavid Howells call->service_id = srx->srx_service; 21057494343SDavid Howells now = ktime_get_real(); 21157494343SDavid Howells call->acks_latest_ts = now; 21257494343SDavid Howells call->cong_tstamp = now; 213999b69f8SDavid Howells 214999b69f8SDavid Howells _leave(" = %p", call); 215999b69f8SDavid Howells return call; 216999b69f8SDavid Howells } 217999b69f8SDavid Howells 218999b69f8SDavid Howells /* 219248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 220999b69f8SDavid Howells */ 221248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 222999b69f8SDavid Howells { 223a158bdd3SDavid Howells unsigned long now = jiffies; 224a158bdd3SDavid Howells unsigned long j = now + MAX_JIFFY_OFFSET; 225999b69f8SDavid Howells 226530403d9SDavid Howells call->delay_ack_at = j; 227bd1fdf8cSDavid Howells call->ack_lost_at = j; 228a158bdd3SDavid Howells call->resend_at = j; 229a158bdd3SDavid Howells call->ping_at = j; 230a158bdd3SDavid Howells call->expect_rx_by = j; 231a158bdd3SDavid Howells call->expect_req_by = j; 232a158bdd3SDavid Howells call->expect_term_by = j; 233a158bdd3SDavid Howells call->timer.expires = now; 2348c3e34a4SDavid Howells } 2358c3e34a4SDavid Howells 2368c3e34a4SDavid Howells /* 237b7a7d674SDavid Howells * Wait for a call slot to become available. 238b7a7d674SDavid Howells */ 239b7a7d674SDavid Howells static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp) 240b7a7d674SDavid Howells { 241b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 242b7a7d674SDavid Howells 243b7a7d674SDavid Howells if (p->kernel) 244b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 245b7a7d674SDavid Howells if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) { 246b7a7d674SDavid Howells down(limiter); 247b7a7d674SDavid Howells return limiter; 248b7a7d674SDavid Howells } 249b7a7d674SDavid Howells return down_interruptible(limiter) < 0 ? NULL : limiter; 250b7a7d674SDavid Howells } 251b7a7d674SDavid Howells 252b7a7d674SDavid Howells /* 253b7a7d674SDavid Howells * Release a call slot. 254b7a7d674SDavid Howells */ 255b7a7d674SDavid Howells static void rxrpc_put_call_slot(struct rxrpc_call *call) 256b7a7d674SDavid Howells { 257b7a7d674SDavid Howells struct semaphore *limiter = &rxrpc_call_limiter; 258b7a7d674SDavid Howells 259b7a7d674SDavid Howells if (test_bit(RXRPC_CALL_KERNEL, &call->flags)) 260b7a7d674SDavid Howells limiter = &rxrpc_kernel_call_limiter; 261b7a7d674SDavid Howells up(limiter); 262b7a7d674SDavid Howells } 263b7a7d674SDavid Howells 264b7a7d674SDavid Howells /* 265540b1c48SDavid Howells * Set up a call for the given parameters. 266540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 267540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2688c3e34a4SDavid Howells */ 2698c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 27019ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 271999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 27248124178SDavid Howells struct rxrpc_call_params *p, 273a25e21f0SDavid Howells gfp_t gfp, 274a25e21f0SDavid Howells unsigned int debug_id) 275540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 27688f2a825SDavid Howells __acquires(&call->user_mutex) 2778c3e34a4SDavid Howells { 2788c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 279d3be4d24SDavid Howells struct rxrpc_net *rxnet; 280b7a7d674SDavid Howells struct semaphore *limiter; 2818c3e34a4SDavid Howells struct rb_node *parent, **pp; 282e34d4234SDavid Howells const void *here = __builtin_return_address(0); 283999b69f8SDavid Howells int ret; 2848c3e34a4SDavid Howells 28548124178SDavid Howells _enter("%p,%lx", rx, p->user_call_ID); 2868c3e34a4SDavid Howells 287b7a7d674SDavid Howells limiter = rxrpc_get_call_slot(p, gfp); 288b0f571ecSDavid Howells if (!limiter) { 289b0f571ecSDavid Howells release_sock(&rx->sk); 290b7a7d674SDavid Howells return ERR_PTR(-ERESTARTSYS); 291b0f571ecSDavid Howells } 292b7a7d674SDavid Howells 293a25e21f0SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id); 2948c3e34a4SDavid Howells if (IS_ERR(call)) { 295540b1c48SDavid Howells release_sock(&rx->sk); 296b7a7d674SDavid Howells up(limiter); 2978c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 2988c3e34a4SDavid Howells return call; 2998c3e34a4SDavid Howells } 3008c3e34a4SDavid Howells 301e138aa7dSDavid Howells call->interruptibility = p->interruptibility; 30248124178SDavid Howells call->tx_total_len = p->tx_total_len; 30348c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_new_client, 304a0575429SDavid Howells refcount_read(&call->ref), 30548124178SDavid Howells here, (const void *)p->user_call_ID); 306b7a7d674SDavid Howells if (p->kernel) 307b7a7d674SDavid Howells __set_bit(RXRPC_CALL_KERNEL, &call->flags); 308e34d4234SDavid Howells 309540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 310540b1c48SDavid Howells * will be acting outside the socket lock. 311540b1c48SDavid Howells */ 312540b1c48SDavid Howells mutex_lock(&call->user_mutex); 313540b1c48SDavid Howells 314999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 3158c3e34a4SDavid Howells write_lock(&rx->call_lock); 3168c3e34a4SDavid Howells 3178c3e34a4SDavid Howells pp = &rx->calls.rb_node; 3188c3e34a4SDavid Howells parent = NULL; 3198c3e34a4SDavid Howells while (*pp) { 3208c3e34a4SDavid Howells parent = *pp; 3218c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 3228c3e34a4SDavid Howells 32348124178SDavid Howells if (p->user_call_ID < xcall->user_call_ID) 3248c3e34a4SDavid Howells pp = &(*pp)->rb_left; 32548124178SDavid Howells else if (p->user_call_ID > xcall->user_call_ID) 3268c3e34a4SDavid Howells pp = &(*pp)->rb_right; 3278c3e34a4SDavid Howells else 328357f5ef6SDavid Howells goto error_dup_user_ID; 3298c3e34a4SDavid Howells } 3308c3e34a4SDavid Howells 331248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 33248124178SDavid Howells call->user_call_ID = p->user_call_ID; 333357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 334fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got_userid); 3358c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 3368c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 337248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 338248f219cSDavid Howells 3398c3e34a4SDavid Howells write_unlock(&rx->call_lock); 3408c3e34a4SDavid Howells 341d3be4d24SDavid Howells rxnet = call->rxnet; 342ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 343ad25f5cbSDavid Howells list_add_tail_rcu(&call->link, &rxnet->calls); 344ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 3458c3e34a4SDavid Howells 346540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 347540b1c48SDavid Howells release_sock(&rx->sk); 348540b1c48SDavid Howells 349248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 350248f219cSDavid Howells * including channel number and call ID. 351248f219cSDavid Howells */ 3525e33a23bSDavid Howells ret = rxrpc_connect_call(rx, call, cp, srx, gfp); 353999b69f8SDavid Howells if (ret < 0) 35465550098SDavid Howells goto error_attached_to_socket; 355999b69f8SDavid Howells 35648c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_connected, 357a0575429SDavid Howells refcount_read(&call->ref), here, NULL); 358a84a46d7SDavid Howells 359248f219cSDavid Howells rxrpc_start_call_timer(call); 360248f219cSDavid Howells 3618c3e34a4SDavid Howells _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); 3628c3e34a4SDavid Howells 3638c3e34a4SDavid Howells _leave(" = %p [new]", call); 3648c3e34a4SDavid Howells return call; 3658c3e34a4SDavid Howells 3668c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 3678c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 3688c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 3698c3e34a4SDavid Howells * same time in different threads. 3708c3e34a4SDavid Howells */ 371357f5ef6SDavid Howells error_dup_user_ID: 3728c3e34a4SDavid Howells write_unlock(&rx->call_lock); 373540b1c48SDavid Howells release_sock(&rx->sk); 374357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 37565550098SDavid Howells RX_CALL_DEAD, -EEXIST); 37648c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_error, 377a0575429SDavid Howells refcount_read(&call->ref), here, ERR_PTR(-EEXIST)); 378357f5ef6SDavid Howells rxrpc_release_call(rx, call); 379540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 380357f5ef6SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 38165550098SDavid Howells _leave(" = -EEXIST"); 38265550098SDavid Howells return ERR_PTR(-EEXIST); 38365550098SDavid Howells 38465550098SDavid Howells /* We got an error, but the call is attached to the socket and is in 38565550098SDavid Howells * need of release. However, we might now race with recvmsg() when 38665550098SDavid Howells * completing the call queues it. Return 0 from sys_sendmsg() and 38765550098SDavid Howells * leave the error to recvmsg() to deal with. 38865550098SDavid Howells */ 38965550098SDavid Howells error_attached_to_socket: 39065550098SDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_error, 391a0575429SDavid Howells refcount_read(&call->ref), here, ERR_PTR(ret)); 39265550098SDavid Howells set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); 39365550098SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 39465550098SDavid Howells RX_CALL_DEAD, ret); 39565550098SDavid Howells _leave(" = c=%08x [err]", call->debug_id); 39665550098SDavid Howells return call; 3978c3e34a4SDavid Howells } 3988c3e34a4SDavid Howells 3998c3e34a4SDavid Howells /* 400248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 401248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 4028c3e34a4SDavid Howells */ 403248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 404248f219cSDavid Howells struct rxrpc_call *call, 40542886ffeSDavid Howells struct sk_buff *skb) 4068c3e34a4SDavid Howells { 407248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 40842886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 409248f219cSDavid Howells u32 chan; 4108c3e34a4SDavid Howells 411248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 4128c3e34a4SDavid Howells 413248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 414248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 415248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 416248f219cSDavid Howells call->cid = sp->hdr.cid; 417248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 41857494343SDavid Howells call->cong_tstamp = skb->tstamp; 4198c3e34a4SDavid Howells 420248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 421248f219cSDavid Howells * only defending against the data_ready handler (which we're called 422248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 423248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 424248f219cSDavid Howells * call pointer). 4258c3e34a4SDavid Howells */ 426248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 427248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 428248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 429a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 4308c3e34a4SDavid Howells 43185f32278SDavid Howells spin_lock(&conn->params.peer->lock); 432f3344303SDavid Howells hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets); 43385f32278SDavid Howells spin_unlock(&conn->params.peer->lock); 4348c3e34a4SDavid Howells 4358c3e34a4SDavid Howells _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); 4368c3e34a4SDavid Howells 437248f219cSDavid Howells rxrpc_start_call_timer(call); 438248f219cSDavid Howells _leave(""); 4398c3e34a4SDavid Howells } 4408c3e34a4SDavid Howells 4418c3e34a4SDavid Howells /* 4428d94aa38SDavid Howells * Queue a call's work processor, getting a ref to pass to the work queue. 4438d94aa38SDavid Howells */ 4448d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call) 4458d94aa38SDavid Howells { 4468d94aa38SDavid Howells const void *here = __builtin_return_address(0); 447a0575429SDavid Howells int n; 448a0575429SDavid Howells 449a0575429SDavid Howells if (!__refcount_inc_not_zero(&call->ref, &n)) 4508d94aa38SDavid Howells return false; 4518d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 45248c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1, 45348c9e0ecSDavid Howells here, NULL); 4548d94aa38SDavid Howells else 4558d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4568d94aa38SDavid Howells return true; 4578d94aa38SDavid Howells } 4588d94aa38SDavid Howells 4598d94aa38SDavid Howells /* 4608d94aa38SDavid Howells * Queue a call's work processor, passing the callers ref to the work queue. 4618d94aa38SDavid Howells */ 4628d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call) 4638d94aa38SDavid Howells { 4648d94aa38SDavid Howells const void *here = __builtin_return_address(0); 465a0575429SDavid Howells int n = refcount_read(&call->ref); 4668d94aa38SDavid Howells ASSERTCMP(n, >=, 1); 4678d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 46848c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n, 46948c9e0ecSDavid Howells here, NULL); 4708d94aa38SDavid Howells else 4718d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 4728d94aa38SDavid Howells return true; 4738d94aa38SDavid Howells } 4748d94aa38SDavid Howells 4758d94aa38SDavid Howells /* 476e34d4234SDavid Howells * Note the re-emergence of a call. 477e34d4234SDavid Howells */ 478e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call) 479e34d4234SDavid Howells { 480e34d4234SDavid Howells const void *here = __builtin_return_address(0); 481e34d4234SDavid Howells if (call) { 482a0575429SDavid Howells int n = refcount_read(&call->ref); 483e34d4234SDavid Howells 48448c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n, 48548c9e0ecSDavid Howells here, NULL); 486e34d4234SDavid Howells } 487e34d4234SDavid Howells } 488e34d4234SDavid Howells 4894a7f62f9SDavid Howells bool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 4904a7f62f9SDavid Howells { 4914a7f62f9SDavid Howells const void *here = __builtin_return_address(0); 492a0575429SDavid Howells int n; 4934a7f62f9SDavid Howells 494a0575429SDavid Howells if (!__refcount_inc_not_zero(&call->ref, &n)) 4954a7f62f9SDavid Howells return false; 496a0575429SDavid Howells trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL); 4974a7f62f9SDavid Howells return true; 4984a7f62f9SDavid Howells } 4994a7f62f9SDavid Howells 500e34d4234SDavid Howells /* 501e34d4234SDavid Howells * Note the addition of a ref on a call. 502e34d4234SDavid Howells */ 503fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 504e34d4234SDavid Howells { 505e34d4234SDavid Howells const void *here = __builtin_return_address(0); 506a0575429SDavid Howells int n; 507e34d4234SDavid Howells 508a0575429SDavid Howells __refcount_inc(&call->ref, &n); 509a0575429SDavid Howells trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL); 510e34d4234SDavid Howells } 511e34d4234SDavid Howells 512e34d4234SDavid Howells /* 513a641fd00SDavid Howells * Clean up the RxTx skb ring. 514a641fd00SDavid Howells */ 515a641fd00SDavid Howells static void rxrpc_cleanup_ring(struct rxrpc_call *call) 516a641fd00SDavid Howells { 517a641fd00SDavid Howells int i; 518a641fd00SDavid Howells 519a641fd00SDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { 520987db9f7SDavid Howells rxrpc_free_skb(call->rxtx_buffer[i], rxrpc_skb_cleaned); 521a641fd00SDavid Howells call->rxtx_buffer[i] = NULL; 522a641fd00SDavid Howells } 523*5d7edbc9SDavid Howells skb_queue_purge(&call->recvmsg_queue); 524*5d7edbc9SDavid Howells skb_queue_purge(&call->rx_oos_queue); 525a641fd00SDavid Howells } 526a641fd00SDavid Howells 527a641fd00SDavid Howells /* 528248f219cSDavid Howells * Detach a call from its owning socket. 5298c3e34a4SDavid Howells */ 5308d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 5318c3e34a4SDavid Howells { 532a84a46d7SDavid Howells const void *here = __builtin_return_address(0); 533248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 534248f219cSDavid Howells bool put = false; 535248f219cSDavid Howells 536a0575429SDavid Howells _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref)); 537248f219cSDavid Howells 53848c9e0ecSDavid Howells trace_rxrpc_call(call->debug_id, rxrpc_call_release, 539a0575429SDavid Howells refcount_read(&call->ref), 540a84a46d7SDavid Howells here, (const void *)call->flags); 5418c3e34a4SDavid Howells 542a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 543e34d4234SDavid Howells 5448c3e34a4SDavid Howells spin_lock_bh(&call->lock); 5458c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 5468c3e34a4SDavid Howells BUG(); 5478c3e34a4SDavid Howells spin_unlock_bh(&call->lock); 5488c3e34a4SDavid Howells 549b7a7d674SDavid Howells rxrpc_put_call_slot(call); 5504a7f62f9SDavid Howells rxrpc_delete_call_timer(call); 5518c3e34a4SDavid Howells 552248f219cSDavid Howells /* Make sure we don't get any more notifications */ 553248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 554e653cfe4SDavid Howells 555248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 5568c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 5578c3e34a4SDavid Howells call, call->events, call->flags); 558248f219cSDavid Howells list_del(&call->recvmsg_link); 559248f219cSDavid Howells put = true; 560248f219cSDavid Howells } 561248f219cSDavid Howells 562248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 563248f219cSDavid Howells call->recvmsg_link.next = NULL; 564248f219cSDavid Howells call->recvmsg_link.prev = NULL; 565248f219cSDavid Howells 566248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 567248f219cSDavid Howells if (put) 568248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 569248f219cSDavid Howells 570248f219cSDavid Howells write_lock(&rx->call_lock); 571248f219cSDavid Howells 572248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 5738c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 5748c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 5758d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid); 5768c3e34a4SDavid Howells } 5778c3e34a4SDavid Howells 578248f219cSDavid Howells list_del(&call->sock_link); 579248f219cSDavid Howells write_unlock(&rx->call_lock); 5808c3e34a4SDavid Howells 581248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 5828c3e34a4SDavid Howells 5835273a191SDavid Howells if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 584e653cfe4SDavid Howells rxrpc_disconnect_call(call); 58591fcfbe8SDavid Howells if (call->security) 58691fcfbe8SDavid Howells call->security->free_call_crypto(call); 5878c3e34a4SDavid Howells _leave(""); 5888c3e34a4SDavid Howells } 5898c3e34a4SDavid Howells 5908c3e34a4SDavid Howells /* 5918c3e34a4SDavid Howells * release all the calls associated with a socket 5928c3e34a4SDavid Howells */ 5938c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 5948c3e34a4SDavid Howells { 5958c3e34a4SDavid Howells struct rxrpc_call *call; 5968c3e34a4SDavid Howells 5978c3e34a4SDavid Howells _enter("%p", rx); 5988c3e34a4SDavid Howells 5990360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 6000360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 6010360da6dSDavid Howells struct rxrpc_call, accept_link); 6020360da6dSDavid Howells list_del(&call->accept_link); 6033a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 6040360da6dSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 6050360da6dSDavid Howells } 6060360da6dSDavid Howells 607248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 608248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 609248f219cSDavid Howells struct rxrpc_call, sock_link); 610248f219cSDavid Howells rxrpc_get_call(call, rxrpc_call_got); 6113a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 61226cb02aaSDavid Howells rxrpc_send_abort_packet(call); 6138d94aa38SDavid Howells rxrpc_release_call(rx, call); 614248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 6158c3e34a4SDavid Howells } 6168c3e34a4SDavid Howells 6178c3e34a4SDavid Howells _leave(""); 6188c3e34a4SDavid Howells } 6198c3e34a4SDavid Howells 6208c3e34a4SDavid Howells /* 6218c3e34a4SDavid Howells * release a call 6228c3e34a4SDavid Howells */ 623fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 6248c3e34a4SDavid Howells { 625d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 626e34d4234SDavid Howells const void *here = __builtin_return_address(0); 62748c9e0ecSDavid Howells unsigned int debug_id = call->debug_id; 628a0575429SDavid Howells bool dead; 6292ab27215SDavid Howells int n; 630e34d4234SDavid Howells 6318c3e34a4SDavid Howells ASSERT(call != NULL); 6328c3e34a4SDavid Howells 633a0575429SDavid Howells dead = __refcount_dec_and_test(&call->ref, &n); 63448c9e0ecSDavid Howells trace_rxrpc_call(debug_id, op, n, here, NULL); 635a0575429SDavid Howells if (dead) { 6368c3e34a4SDavid Howells _debug("call %d dead", call->debug_id); 637248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 638e34d4234SDavid Howells 6392baec2c3SDavid Howells if (!list_empty(&call->link)) { 640ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 641248f219cSDavid Howells list_del_init(&call->link); 642ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 6432baec2c3SDavid Howells } 644e34d4234SDavid Howells 6458d94aa38SDavid Howells rxrpc_cleanup_call(call); 646e34d4234SDavid Howells } 6478c3e34a4SDavid Howells } 6488c3e34a4SDavid Howells 6498c3e34a4SDavid Howells /* 650963485d4SDavid Howells * Final call destruction - but must be done in process context. 651dee46364SDavid Howells */ 652963485d4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work) 653dee46364SDavid Howells { 654963485d4SDavid Howells struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor); 655d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 656dee46364SDavid Howells 6574a7f62f9SDavid Howells rxrpc_delete_call_timer(call); 6584a7f62f9SDavid Howells 6595273a191SDavid Howells rxrpc_put_connection(call->conn); 660df5d8bf7SDavid Howells rxrpc_put_peer(call->peer); 661248f219cSDavid Howells kfree(call->rxtx_buffer); 662248f219cSDavid Howells kfree(call->rxtx_annotations); 663dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 664d3be4d24SDavid Howells if (atomic_dec_and_test(&rxnet->nr_calls)) 6655bb053beSLinus Torvalds wake_up_var(&rxnet->nr_calls); 666dee46364SDavid Howells } 667dee46364SDavid Howells 668dee46364SDavid Howells /* 669963485d4SDavid Howells * Final call destruction under RCU. 670963485d4SDavid Howells */ 671963485d4SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu) 672963485d4SDavid Howells { 673963485d4SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 674963485d4SDavid Howells 675963485d4SDavid Howells if (in_softirq()) { 676963485d4SDavid Howells INIT_WORK(&call->processor, rxrpc_destroy_call); 677963485d4SDavid Howells if (!rxrpc_queue_work(&call->processor)) 678963485d4SDavid Howells BUG(); 679963485d4SDavid Howells } else { 680963485d4SDavid Howells rxrpc_destroy_call(&call->processor); 681963485d4SDavid Howells } 682963485d4SDavid Howells } 683963485d4SDavid Howells 684963485d4SDavid Howells /* 6858c3e34a4SDavid Howells * clean up a call 6868c3e34a4SDavid Howells */ 68700e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 6888c3e34a4SDavid Howells { 689248f219cSDavid Howells _net("DESTROY CALL %d", call->debug_id); 6908c3e34a4SDavid Howells 6918c3e34a4SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 6928c3e34a4SDavid Howells 6938d94aa38SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 6948c3e34a4SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 6958c3e34a4SDavid Howells 696a641fd00SDavid Howells rxrpc_cleanup_ring(call); 697987db9f7SDavid Howells rxrpc_free_skb(call->tx_pending, rxrpc_skb_cleaned); 6988c3e34a4SDavid Howells 699dee46364SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_destroy_call); 7008c3e34a4SDavid Howells } 7018c3e34a4SDavid Howells 7028c3e34a4SDavid Howells /* 7032baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 7042baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 7052baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 7068c3e34a4SDavid Howells */ 7072baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 7088c3e34a4SDavid Howells { 7098c3e34a4SDavid Howells struct rxrpc_call *call; 7108c3e34a4SDavid Howells 7118c3e34a4SDavid Howells _enter(""); 7128d94aa38SDavid Howells 713b1302342SDavid Howells if (!list_empty(&rxnet->calls)) { 714ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 7158c3e34a4SDavid Howells 7162baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 717b1302342SDavid Howells call = list_entry(rxnet->calls.next, 718b1302342SDavid Howells struct rxrpc_call, link); 7198c3e34a4SDavid Howells _debug("Zapping call %p", call); 7208c3e34a4SDavid Howells 721e34d4234SDavid Howells rxrpc_see_call(call); 7228c3e34a4SDavid Howells list_del_init(&call->link); 7238c3e34a4SDavid Howells 724248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 725a0575429SDavid Howells call, refcount_read(&call->ref), 7268c3e34a4SDavid Howells rxrpc_call_states[call->state], 7278c3e34a4SDavid Howells call->flags, call->events); 7288c3e34a4SDavid Howells 729ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 7308c3e34a4SDavid Howells cond_resched(); 731ad25f5cbSDavid Howells spin_lock_bh(&rxnet->call_lock); 7328c3e34a4SDavid Howells } 7338c3e34a4SDavid Howells 734ad25f5cbSDavid Howells spin_unlock_bh(&rxnet->call_lock); 735b1302342SDavid Howells } 736d3be4d24SDavid Howells 737d3be4d24SDavid Howells atomic_dec(&rxnet->nr_calls); 7385bb053beSLinus Torvalds wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls)); 7398c3e34a4SDavid Howells } 740