18c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling 28c3e34a4SDavid Howells * 38c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 48c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 58c3e34a4SDavid Howells * 68c3e34a4SDavid Howells * This program is free software; you can redistribute it and/or 78c3e34a4SDavid Howells * modify it under the terms of the GNU General Public License 88c3e34a4SDavid Howells * as published by the Free Software Foundation; either version 98c3e34a4SDavid Howells * 2 of the License, or (at your option) any later version. 108c3e34a4SDavid Howells */ 118c3e34a4SDavid Howells 128c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 138c3e34a4SDavid Howells 148c3e34a4SDavid Howells #include <linux/slab.h> 158c3e34a4SDavid Howells #include <linux/module.h> 168c3e34a4SDavid Howells #include <linux/circ_buf.h> 178c3e34a4SDavid Howells #include <linux/spinlock_types.h> 188c3e34a4SDavid Howells #include <net/sock.h> 198c3e34a4SDavid Howells #include <net/af_rxrpc.h> 208c3e34a4SDavid Howells #include "ar-internal.h" 218c3e34a4SDavid Howells 228c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = { 23999b69f8SDavid Howells [RXRPC_CALL_UNINITIALISED] = "Uninit ", 24999b69f8SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn", 258c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", 268c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", 278c3e34a4SDavid Howells [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", 2800e90712SDavid Howells [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc", 298c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SECURING] = "SvSecure", 308c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept", 318c3e34a4SDavid Howells [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", 328c3e34a4SDavid Howells [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", 338c3e34a4SDavid Howells [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", 348c3e34a4SDavid Howells [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", 358c3e34a4SDavid Howells [RXRPC_CALL_COMPLETE] = "Complete", 36f5c17aaeSDavid Howells }; 37f5c17aaeSDavid Howells 38f5c17aaeSDavid Howells const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = { 39f5c17aaeSDavid Howells [RXRPC_CALL_SUCCEEDED] = "Complete", 408c3e34a4SDavid Howells [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", 418c3e34a4SDavid Howells [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", 42f5c17aaeSDavid Howells [RXRPC_CALL_LOCAL_ERROR] = "LocError", 438c3e34a4SDavid Howells [RXRPC_CALL_NETWORK_ERROR] = "NetError", 448c3e34a4SDavid Howells }; 458c3e34a4SDavid Howells 468c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar; 478c3e34a4SDavid 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) { 55a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies); 56a158bdd3SDavid Howells rxrpc_queue_call(call); 57fc7ab6d2SDavid Howells } 58248f219cSDavid Howells } 598c3e34a4SDavid Howells 609faaff59SDavid Howells static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; 618c3e34a4SDavid Howells 628c3e34a4SDavid Howells /* 638c3e34a4SDavid Howells * find an extant server call 648c3e34a4SDavid Howells * - called in process context with IRQs enabled 658c3e34a4SDavid Howells */ 668c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, 678c3e34a4SDavid Howells unsigned long user_call_ID) 688c3e34a4SDavid Howells { 698c3e34a4SDavid Howells struct rxrpc_call *call; 708c3e34a4SDavid Howells struct rb_node *p; 718c3e34a4SDavid Howells 728c3e34a4SDavid Howells _enter("%p,%lx", rx, user_call_ID); 738c3e34a4SDavid Howells 748c3e34a4SDavid Howells read_lock(&rx->call_lock); 758c3e34a4SDavid Howells 768c3e34a4SDavid Howells p = rx->calls.rb_node; 778c3e34a4SDavid Howells while (p) { 788c3e34a4SDavid Howells call = rb_entry(p, struct rxrpc_call, sock_node); 798c3e34a4SDavid Howells 808c3e34a4SDavid Howells if (user_call_ID < call->user_call_ID) 818c3e34a4SDavid Howells p = p->rb_left; 828c3e34a4SDavid Howells else if (user_call_ID > call->user_call_ID) 838c3e34a4SDavid Howells p = p->rb_right; 848c3e34a4SDavid Howells else 858c3e34a4SDavid Howells goto found_extant_call; 868c3e34a4SDavid Howells } 878c3e34a4SDavid Howells 888c3e34a4SDavid Howells read_unlock(&rx->call_lock); 898c3e34a4SDavid Howells _leave(" = NULL"); 908c3e34a4SDavid Howells return NULL; 918c3e34a4SDavid Howells 928c3e34a4SDavid Howells found_extant_call: 93fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got); 948c3e34a4SDavid Howells read_unlock(&rx->call_lock); 958c3e34a4SDavid Howells _leave(" = %p [%d]", call, atomic_read(&call->usage)); 968c3e34a4SDavid Howells return call; 978c3e34a4SDavid Howells } 988c3e34a4SDavid Howells 998c3e34a4SDavid Howells /* 1008c3e34a4SDavid Howells * allocate a new call 1018c3e34a4SDavid Howells */ 102a25e21f0SDavid Howells struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp, 103a25e21f0SDavid Howells unsigned int debug_id) 1048c3e34a4SDavid Howells { 1058c3e34a4SDavid Howells struct rxrpc_call *call; 106d3be4d24SDavid Howells struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); 1078c3e34a4SDavid Howells 1088c3e34a4SDavid Howells call = kmem_cache_zalloc(rxrpc_call_jar, gfp); 1098c3e34a4SDavid Howells if (!call) 1108c3e34a4SDavid Howells return NULL; 1118c3e34a4SDavid Howells 112248f219cSDavid Howells call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE, 113248f219cSDavid Howells sizeof(struct sk_buff *), 1148c3e34a4SDavid Howells gfp); 115248f219cSDavid Howells if (!call->rxtx_buffer) 116248f219cSDavid Howells goto nomem; 1178c3e34a4SDavid Howells 118248f219cSDavid Howells call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp); 119248f219cSDavid Howells if (!call->rxtx_annotations) 120248f219cSDavid Howells goto nomem_2; 121248f219cSDavid Howells 122540b1c48SDavid Howells mutex_init(&call->user_mutex); 1239faaff59SDavid Howells 1249faaff59SDavid Howells /* Prevent lockdep reporting a deadlock false positive between the afs 1259faaff59SDavid Howells * filesystem and sys_sendmsg() via the mmap sem. 1269faaff59SDavid Howells */ 1279faaff59SDavid Howells if (rx->sk.sk_kern_sock) 1289faaff59SDavid Howells lockdep_set_class(&call->user_mutex, 1299faaff59SDavid Howells &rxrpc_call_user_mutex_lock_class_key); 1309faaff59SDavid Howells 131e99e88a9SKees Cook timer_setup(&call->timer, rxrpc_call_timer_expired, 0); 1328c3e34a4SDavid Howells INIT_WORK(&call->processor, &rxrpc_process_call); 133999b69f8SDavid Howells INIT_LIST_HEAD(&call->link); 13445025bceSDavid Howells INIT_LIST_HEAD(&call->chan_wait_link); 1358c3e34a4SDavid Howells INIT_LIST_HEAD(&call->accept_link); 136248f219cSDavid Howells INIT_LIST_HEAD(&call->recvmsg_link); 137248f219cSDavid Howells INIT_LIST_HEAD(&call->sock_link); 13845025bceSDavid Howells init_waitqueue_head(&call->waitq); 1398c3e34a4SDavid Howells spin_lock_init(&call->lock); 14020acbd9aSDavid Howells spin_lock_init(&call->notify_lock); 141c1e15b49SDavid Howells spin_lock_init(&call->input_lock); 1428c3e34a4SDavid Howells rwlock_init(&call->state_lock); 1438c3e34a4SDavid Howells atomic_set(&call->usage, 1); 144a25e21f0SDavid Howells call->debug_id = debug_id; 145e754eba6SDavid Howells call->tx_total_len = -1; 146a158bdd3SDavid Howells call->next_rx_timo = 20 * HZ; 147a158bdd3SDavid Howells call->next_req_timo = 1 * HZ; 1488c3e34a4SDavid Howells 1498c3e34a4SDavid Howells memset(&call->sock_node, 0xed, sizeof(call->sock_node)); 1508c3e34a4SDavid Howells 151248f219cSDavid Howells /* Leave space in the ring to handle a maxed-out jumbo packet */ 15275e42126SDavid Howells call->rx_winsize = rxrpc_rx_window_size; 153248f219cSDavid Howells call->tx_winsize = 16; 154248f219cSDavid Howells call->rx_expect_next = 1; 15557494343SDavid Howells 15657494343SDavid Howells call->cong_cwnd = 2; 15757494343SDavid Howells call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1; 158d3be4d24SDavid Howells 159d3be4d24SDavid Howells call->rxnet = rxnet; 160d3be4d24SDavid Howells atomic_inc(&rxnet->nr_calls); 1618c3e34a4SDavid Howells return call; 162248f219cSDavid Howells 163248f219cSDavid Howells nomem_2: 164248f219cSDavid Howells kfree(call->rxtx_buffer); 165248f219cSDavid Howells nomem: 166248f219cSDavid Howells kmem_cache_free(rxrpc_call_jar, call); 167248f219cSDavid Howells return NULL; 1688c3e34a4SDavid Howells } 1698c3e34a4SDavid Howells 1708c3e34a4SDavid Howells /* 171999b69f8SDavid Howells * Allocate a new client call. 1728c3e34a4SDavid Howells */ 1739faaff59SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, 1749faaff59SDavid Howells struct sockaddr_rxrpc *srx, 175a25e21f0SDavid Howells gfp_t gfp, 176a25e21f0SDavid Howells unsigned int debug_id) 1778c3e34a4SDavid Howells { 1788c3e34a4SDavid Howells struct rxrpc_call *call; 17957494343SDavid Howells ktime_t now; 1808c3e34a4SDavid Howells 1818c3e34a4SDavid Howells _enter(""); 1828c3e34a4SDavid Howells 183a25e21f0SDavid Howells call = rxrpc_alloc_call(rx, gfp, debug_id); 1848c3e34a4SDavid Howells if (!call) 1858c3e34a4SDavid Howells return ERR_PTR(-ENOMEM); 186999b69f8SDavid Howells call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; 187999b69f8SDavid Howells call->service_id = srx->srx_service; 18871f3ca40SDavid Howells call->tx_phase = true; 18957494343SDavid Howells now = ktime_get_real(); 19057494343SDavid Howells call->acks_latest_ts = now; 19157494343SDavid Howells call->cong_tstamp = now; 192999b69f8SDavid Howells 193999b69f8SDavid Howells _leave(" = %p", call); 194999b69f8SDavid Howells return call; 195999b69f8SDavid Howells } 196999b69f8SDavid Howells 197999b69f8SDavid Howells /* 198248f219cSDavid Howells * Initiate the call ack/resend/expiry timer. 199999b69f8SDavid Howells */ 200248f219cSDavid Howells static void rxrpc_start_call_timer(struct rxrpc_call *call) 201999b69f8SDavid Howells { 202a158bdd3SDavid Howells unsigned long now = jiffies; 203a158bdd3SDavid Howells unsigned long j = now + MAX_JIFFY_OFFSET; 204999b69f8SDavid Howells 205a158bdd3SDavid Howells call->ack_at = j; 206bd1fdf8cSDavid Howells call->ack_lost_at = j; 207a158bdd3SDavid Howells call->resend_at = j; 208a158bdd3SDavid Howells call->ping_at = j; 209a158bdd3SDavid Howells call->expect_rx_by = j; 210a158bdd3SDavid Howells call->expect_req_by = j; 211a158bdd3SDavid Howells call->expect_term_by = j; 212a158bdd3SDavid Howells call->timer.expires = now; 2138c3e34a4SDavid Howells } 2148c3e34a4SDavid Howells 2158c3e34a4SDavid Howells /* 216540b1c48SDavid Howells * Set up a call for the given parameters. 217540b1c48SDavid Howells * - Called with the socket lock held, which it must release. 218540b1c48SDavid Howells * - If it returns a call, the call's lock will need releasing by the caller. 2198c3e34a4SDavid Howells */ 2208c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, 22119ffa01cSDavid Howells struct rxrpc_conn_parameters *cp, 222999b69f8SDavid Howells struct sockaddr_rxrpc *srx, 22348124178SDavid Howells struct rxrpc_call_params *p, 224a25e21f0SDavid Howells gfp_t gfp, 225a25e21f0SDavid Howells unsigned int debug_id) 226540b1c48SDavid Howells __releases(&rx->sk.sk_lock.slock) 22788f2a825SDavid Howells __acquires(&call->user_mutex) 2288c3e34a4SDavid Howells { 2298c3e34a4SDavid Howells struct rxrpc_call *call, *xcall; 230d3be4d24SDavid Howells struct rxrpc_net *rxnet; 2318c3e34a4SDavid Howells struct rb_node *parent, **pp; 232e34d4234SDavid Howells const void *here = __builtin_return_address(0); 233999b69f8SDavid Howells int ret; 2348c3e34a4SDavid Howells 23548124178SDavid Howells _enter("%p,%lx", rx, p->user_call_ID); 2368c3e34a4SDavid Howells 237a25e21f0SDavid Howells call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id); 2388c3e34a4SDavid Howells if (IS_ERR(call)) { 239540b1c48SDavid Howells release_sock(&rx->sk); 2408c3e34a4SDavid Howells _leave(" = %ld", PTR_ERR(call)); 2418c3e34a4SDavid Howells return call; 2428c3e34a4SDavid Howells } 2438c3e34a4SDavid Howells 24448124178SDavid Howells call->tx_total_len = p->tx_total_len; 245a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage), 24648124178SDavid Howells here, (const void *)p->user_call_ID); 247e34d4234SDavid Howells 248540b1c48SDavid Howells /* We need to protect a partially set up call against the user as we 249540b1c48SDavid Howells * will be acting outside the socket lock. 250540b1c48SDavid Howells */ 251540b1c48SDavid Howells mutex_lock(&call->user_mutex); 252540b1c48SDavid Howells 253999b69f8SDavid Howells /* Publish the call, even though it is incompletely set up as yet */ 2548c3e34a4SDavid Howells write_lock(&rx->call_lock); 2558c3e34a4SDavid Howells 2568c3e34a4SDavid Howells pp = &rx->calls.rb_node; 2578c3e34a4SDavid Howells parent = NULL; 2588c3e34a4SDavid Howells while (*pp) { 2598c3e34a4SDavid Howells parent = *pp; 2608c3e34a4SDavid Howells xcall = rb_entry(parent, struct rxrpc_call, sock_node); 2618c3e34a4SDavid Howells 26248124178SDavid Howells if (p->user_call_ID < xcall->user_call_ID) 2638c3e34a4SDavid Howells pp = &(*pp)->rb_left; 26448124178SDavid Howells else if (p->user_call_ID > xcall->user_call_ID) 2658c3e34a4SDavid Howells pp = &(*pp)->rb_right; 2668c3e34a4SDavid Howells else 267357f5ef6SDavid Howells goto error_dup_user_ID; 2688c3e34a4SDavid Howells } 2698c3e34a4SDavid Howells 270248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 27148124178SDavid Howells call->user_call_ID = p->user_call_ID; 272357f5ef6SDavid Howells __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); 273fff72429SDavid Howells rxrpc_get_call(call, rxrpc_call_got_userid); 2748c3e34a4SDavid Howells rb_link_node(&call->sock_node, parent, pp); 2758c3e34a4SDavid Howells rb_insert_color(&call->sock_node, &rx->calls); 276248f219cSDavid Howells list_add(&call->sock_link, &rx->sock_calls); 277248f219cSDavid Howells 2788c3e34a4SDavid Howells write_unlock(&rx->call_lock); 2798c3e34a4SDavid Howells 280d3be4d24SDavid Howells rxnet = call->rxnet; 2812baec2c3SDavid Howells write_lock(&rxnet->call_lock); 2822baec2c3SDavid Howells list_add_tail(&call->link, &rxnet->calls); 2832baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 2848c3e34a4SDavid Howells 285540b1c48SDavid Howells /* From this point on, the call is protected by its own lock. */ 286540b1c48SDavid Howells release_sock(&rx->sk); 287540b1c48SDavid Howells 288248f219cSDavid Howells /* Set up or get a connection record and set the protocol parameters, 289248f219cSDavid Howells * including channel number and call ID. 290248f219cSDavid Howells */ 2915e33a23bSDavid Howells ret = rxrpc_connect_call(rx, call, cp, srx, gfp); 292999b69f8SDavid Howells if (ret < 0) 293999b69f8SDavid Howells goto error; 294999b69f8SDavid Howells 295a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage), 29654fde423SDavid Howells here, NULL); 297a84a46d7SDavid Howells 298248f219cSDavid Howells rxrpc_start_call_timer(call); 299248f219cSDavid Howells 3008c3e34a4SDavid Howells _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); 3018c3e34a4SDavid Howells 3028c3e34a4SDavid Howells _leave(" = %p [new]", call); 3038c3e34a4SDavid Howells return call; 3048c3e34a4SDavid Howells 3058c3e34a4SDavid Howells /* We unexpectedly found the user ID in the list after taking 3068c3e34a4SDavid Howells * the call_lock. This shouldn't happen unless the user races 3078c3e34a4SDavid Howells * with itself and tries to add the same user ID twice at the 3088c3e34a4SDavid Howells * same time in different threads. 3098c3e34a4SDavid Howells */ 310357f5ef6SDavid Howells error_dup_user_ID: 3118c3e34a4SDavid Howells write_unlock(&rx->call_lock); 312540b1c48SDavid Howells release_sock(&rx->sk); 3138d94aa38SDavid Howells ret = -EEXIST; 314357f5ef6SDavid Howells 315357f5ef6SDavid Howells error: 316357f5ef6SDavid Howells __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 317357f5ef6SDavid Howells RX_CALL_DEAD, ret); 318a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage), 319a84a46d7SDavid Howells here, ERR_PTR(ret)); 320357f5ef6SDavid Howells rxrpc_release_call(rx, call); 321540b1c48SDavid Howells mutex_unlock(&call->user_mutex); 322357f5ef6SDavid Howells rxrpc_put_call(call, rxrpc_call_put); 323357f5ef6SDavid Howells _leave(" = %d", ret); 324357f5ef6SDavid Howells return ERR_PTR(ret); 3258c3e34a4SDavid Howells } 3268c3e34a4SDavid Howells 3278c3e34a4SDavid Howells /* 328248f219cSDavid Howells * Set up an incoming call. call->conn points to the connection. 329248f219cSDavid Howells * This is called in BH context and isn't allowed to fail. 3308c3e34a4SDavid Howells */ 331248f219cSDavid Howells void rxrpc_incoming_call(struct rxrpc_sock *rx, 332248f219cSDavid Howells struct rxrpc_call *call, 33342886ffeSDavid Howells struct sk_buff *skb) 3348c3e34a4SDavid Howells { 335248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 33642886ffeSDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 337248f219cSDavid Howells u32 chan; 3388c3e34a4SDavid Howells 339248f219cSDavid Howells _enter(",%d", call->conn->debug_id); 3408c3e34a4SDavid Howells 341248f219cSDavid Howells rcu_assign_pointer(call->socket, rx); 342248f219cSDavid Howells call->call_id = sp->hdr.callNumber; 343248f219cSDavid Howells call->service_id = sp->hdr.serviceId; 344248f219cSDavid Howells call->cid = sp->hdr.cid; 345248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_ACCEPTING; 346248f219cSDavid Howells if (sp->hdr.securityIndex > 0) 347248f219cSDavid Howells call->state = RXRPC_CALL_SERVER_SECURING; 34857494343SDavid Howells call->cong_tstamp = skb->tstamp; 3498c3e34a4SDavid Howells 350248f219cSDavid Howells /* Set the channel for this call. We don't get channel_lock as we're 351248f219cSDavid Howells * only defending against the data_ready handler (which we're called 352248f219cSDavid Howells * from) and the RESPONSE packet parser (which is only really 353248f219cSDavid Howells * interested in call_counter and can cope with a disagreement with the 354248f219cSDavid Howells * call pointer). 3558c3e34a4SDavid Howells */ 356248f219cSDavid Howells chan = sp->hdr.cid & RXRPC_CHANNELMASK; 357248f219cSDavid Howells conn->channels[chan].call_counter = call->call_id; 358248f219cSDavid Howells conn->channels[chan].call_id = call->call_id; 359a1399f8bSDavid Howells rcu_assign_pointer(conn->channels[chan].call, call); 3608c3e34a4SDavid Howells 36185f32278SDavid Howells spin_lock(&conn->params.peer->lock); 362f3344303SDavid Howells hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets); 36385f32278SDavid Howells spin_unlock(&conn->params.peer->lock); 3648c3e34a4SDavid Howells 3658c3e34a4SDavid Howells _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); 3668c3e34a4SDavid Howells 367248f219cSDavid Howells rxrpc_start_call_timer(call); 368248f219cSDavid Howells _leave(""); 3698c3e34a4SDavid Howells } 3708c3e34a4SDavid Howells 3718c3e34a4SDavid Howells /* 3728d94aa38SDavid Howells * Queue a call's work processor, getting a ref to pass to the work queue. 3738d94aa38SDavid Howells */ 3748d94aa38SDavid Howells bool rxrpc_queue_call(struct rxrpc_call *call) 3758d94aa38SDavid Howells { 3768d94aa38SDavid Howells const void *here = __builtin_return_address(0); 377bfc18e38SMark Rutland int n = atomic_fetch_add_unless(&call->usage, 1, 0); 3788d94aa38SDavid Howells if (n == 0) 3798d94aa38SDavid Howells return false; 3808d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 3812ab27215SDavid Howells trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL); 3828d94aa38SDavid Howells else 3838d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 3848d94aa38SDavid Howells return true; 3858d94aa38SDavid Howells } 3868d94aa38SDavid Howells 3878d94aa38SDavid Howells /* 3888d94aa38SDavid Howells * Queue a call's work processor, passing the callers ref to the work queue. 3898d94aa38SDavid Howells */ 3908d94aa38SDavid Howells bool __rxrpc_queue_call(struct rxrpc_call *call) 3918d94aa38SDavid Howells { 3928d94aa38SDavid Howells const void *here = __builtin_return_address(0); 3938d94aa38SDavid Howells int n = atomic_read(&call->usage); 3948d94aa38SDavid Howells ASSERTCMP(n, >=, 1); 3958d94aa38SDavid Howells if (rxrpc_queue_work(&call->processor)) 3962ab27215SDavid Howells trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL); 3978d94aa38SDavid Howells else 3988d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_noqueue); 3998d94aa38SDavid Howells return true; 4008d94aa38SDavid Howells } 4018d94aa38SDavid Howells 4028d94aa38SDavid Howells /* 403e34d4234SDavid Howells * Note the re-emergence of a call. 404e34d4234SDavid Howells */ 405e34d4234SDavid Howells void rxrpc_see_call(struct rxrpc_call *call) 406e34d4234SDavid Howells { 407e34d4234SDavid Howells const void *here = __builtin_return_address(0); 408e34d4234SDavid Howells if (call) { 409e34d4234SDavid Howells int n = atomic_read(&call->usage); 410e34d4234SDavid Howells 4112ab27215SDavid Howells trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL); 412e34d4234SDavid Howells } 413e34d4234SDavid Howells } 414e34d4234SDavid Howells 415e34d4234SDavid Howells /* 416e34d4234SDavid Howells * Note the addition of a ref on a call. 417e34d4234SDavid Howells */ 418fff72429SDavid Howells void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 419e34d4234SDavid Howells { 420e34d4234SDavid Howells const void *here = __builtin_return_address(0); 421e34d4234SDavid Howells int n = atomic_inc_return(&call->usage); 422e34d4234SDavid Howells 4232ab27215SDavid Howells trace_rxrpc_call(call, op, n, here, NULL); 424e34d4234SDavid Howells } 425e34d4234SDavid Howells 426e34d4234SDavid Howells /* 427248f219cSDavid Howells * Detach a call from its owning socket. 4288c3e34a4SDavid Howells */ 4298d94aa38SDavid Howells void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) 4308c3e34a4SDavid Howells { 431a84a46d7SDavid Howells const void *here = __builtin_return_address(0); 432248f219cSDavid Howells struct rxrpc_connection *conn = call->conn; 433248f219cSDavid Howells bool put = false; 434248f219cSDavid Howells int i; 435248f219cSDavid Howells 436248f219cSDavid Howells _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage)); 437248f219cSDavid Howells 438a84a46d7SDavid Howells trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage), 439a84a46d7SDavid Howells here, (const void *)call->flags); 4408c3e34a4SDavid Howells 441a84a46d7SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 442e34d4234SDavid Howells 4438c3e34a4SDavid Howells spin_lock_bh(&call->lock); 4448c3e34a4SDavid Howells if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) 4458c3e34a4SDavid Howells BUG(); 4468c3e34a4SDavid Howells spin_unlock_bh(&call->lock); 4478c3e34a4SDavid Howells 448248f219cSDavid Howells del_timer_sync(&call->timer); 4498c3e34a4SDavid Howells 450248f219cSDavid Howells /* Make sure we don't get any more notifications */ 451248f219cSDavid Howells write_lock_bh(&rx->recvmsg_lock); 452e653cfe4SDavid Howells 453248f219cSDavid Howells if (!list_empty(&call->recvmsg_link)) { 4548c3e34a4SDavid Howells _debug("unlinking once-pending call %p { e=%lx f=%lx }", 4558c3e34a4SDavid Howells call, call->events, call->flags); 456248f219cSDavid Howells list_del(&call->recvmsg_link); 457248f219cSDavid Howells put = true; 458248f219cSDavid Howells } 459248f219cSDavid Howells 460248f219cSDavid Howells /* list_empty() must return false in rxrpc_notify_socket() */ 461248f219cSDavid Howells call->recvmsg_link.next = NULL; 462248f219cSDavid Howells call->recvmsg_link.prev = NULL; 463248f219cSDavid Howells 464248f219cSDavid Howells write_unlock_bh(&rx->recvmsg_lock); 465248f219cSDavid Howells if (put) 466248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 467248f219cSDavid Howells 468248f219cSDavid Howells write_lock(&rx->call_lock); 469248f219cSDavid Howells 470248f219cSDavid Howells if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { 4718c3e34a4SDavid Howells rb_erase(&call->sock_node, &rx->calls); 4728c3e34a4SDavid Howells memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); 4738d94aa38SDavid Howells rxrpc_put_call(call, rxrpc_call_put_userid); 4748c3e34a4SDavid Howells } 4758c3e34a4SDavid Howells 476248f219cSDavid Howells list_del(&call->sock_link); 477248f219cSDavid Howells write_unlock(&rx->call_lock); 4788c3e34a4SDavid Howells 479248f219cSDavid Howells _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); 4808c3e34a4SDavid Howells 481248f219cSDavid Howells if (conn) 482e653cfe4SDavid Howells rxrpc_disconnect_call(call); 483e653cfe4SDavid Howells 484248f219cSDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { 48571f3ca40SDavid Howells rxrpc_free_skb(call->rxtx_buffer[i], 48671f3ca40SDavid Howells (call->tx_phase ? rxrpc_skb_tx_cleaned : 48771f3ca40SDavid Howells rxrpc_skb_rx_cleaned)); 488248f219cSDavid Howells call->rxtx_buffer[i] = NULL; 4898c3e34a4SDavid Howells } 4908c3e34a4SDavid Howells 4918c3e34a4SDavid Howells _leave(""); 4928c3e34a4SDavid Howells } 4938c3e34a4SDavid Howells 4948c3e34a4SDavid Howells /* 4958c3e34a4SDavid Howells * release all the calls associated with a socket 4968c3e34a4SDavid Howells */ 4978c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) 4988c3e34a4SDavid Howells { 4998c3e34a4SDavid Howells struct rxrpc_call *call; 5008c3e34a4SDavid Howells 5018c3e34a4SDavid Howells _enter("%p", rx); 5028c3e34a4SDavid Howells 5030360da6dSDavid Howells while (!list_empty(&rx->to_be_accepted)) { 5040360da6dSDavid Howells call = list_entry(rx->to_be_accepted.next, 5050360da6dSDavid Howells struct rxrpc_call, accept_link); 5060360da6dSDavid Howells list_del(&call->accept_link); 5073a92789aSDavid Howells rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); 5080360da6dSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5090360da6dSDavid Howells } 5100360da6dSDavid Howells 511248f219cSDavid Howells while (!list_empty(&rx->sock_calls)) { 512248f219cSDavid Howells call = list_entry(rx->sock_calls.next, 513248f219cSDavid Howells struct rxrpc_call, sock_link); 514248f219cSDavid Howells rxrpc_get_call(call, rxrpc_call_got); 5153a92789aSDavid Howells rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); 51626cb02aaSDavid Howells rxrpc_send_abort_packet(call); 5178d94aa38SDavid Howells rxrpc_release_call(rx, call); 518248f219cSDavid Howells rxrpc_put_call(call, rxrpc_call_put); 5198c3e34a4SDavid Howells } 5208c3e34a4SDavid Howells 5218c3e34a4SDavid Howells _leave(""); 5228c3e34a4SDavid Howells } 5238c3e34a4SDavid Howells 5248c3e34a4SDavid Howells /* 5258c3e34a4SDavid Howells * release a call 5268c3e34a4SDavid Howells */ 527fff72429SDavid Howells void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op) 5288c3e34a4SDavid Howells { 529d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 530e34d4234SDavid Howells const void *here = __builtin_return_address(0); 5312ab27215SDavid Howells int n; 532e34d4234SDavid Howells 5338c3e34a4SDavid Howells ASSERT(call != NULL); 5348c3e34a4SDavid Howells 535e34d4234SDavid Howells n = atomic_dec_return(&call->usage); 5362ab27215SDavid Howells trace_rxrpc_call(call, op, n, here, NULL); 537e34d4234SDavid Howells ASSERTCMP(n, >=, 0); 538e34d4234SDavid Howells if (n == 0) { 5398c3e34a4SDavid Howells _debug("call %d dead", call->debug_id); 540248f219cSDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 541e34d4234SDavid Howells 5422baec2c3SDavid Howells if (!list_empty(&call->link)) { 5432baec2c3SDavid Howells write_lock(&rxnet->call_lock); 544248f219cSDavid Howells list_del_init(&call->link); 5452baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 5462baec2c3SDavid Howells } 547e34d4234SDavid Howells 5488d94aa38SDavid Howells rxrpc_cleanup_call(call); 549e34d4234SDavid Howells } 5508c3e34a4SDavid Howells } 5518c3e34a4SDavid Howells 5528c3e34a4SDavid Howells /* 553dee46364SDavid Howells * Final call destruction under RCU. 554dee46364SDavid Howells */ 555dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu) 556dee46364SDavid Howells { 557dee46364SDavid Howells struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); 558d3be4d24SDavid Howells struct rxrpc_net *rxnet = call->rxnet; 559dee46364SDavid Howells 560df5d8bf7SDavid Howells rxrpc_put_peer(call->peer); 561248f219cSDavid Howells kfree(call->rxtx_buffer); 562248f219cSDavid Howells kfree(call->rxtx_annotations); 563dee46364SDavid Howells kmem_cache_free(rxrpc_call_jar, call); 564d3be4d24SDavid Howells if (atomic_dec_and_test(&rxnet->nr_calls)) 5655bb053beSLinus Torvalds wake_up_var(&rxnet->nr_calls); 566dee46364SDavid Howells } 567dee46364SDavid Howells 568dee46364SDavid Howells /* 5698c3e34a4SDavid Howells * clean up a call 5708c3e34a4SDavid Howells */ 57100e90712SDavid Howells void rxrpc_cleanup_call(struct rxrpc_call *call) 5728c3e34a4SDavid Howells { 573248f219cSDavid Howells int i; 5748c3e34a4SDavid Howells 575248f219cSDavid Howells _net("DESTROY CALL %d", call->debug_id); 5768c3e34a4SDavid Howells 5778c3e34a4SDavid Howells memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); 5788c3e34a4SDavid Howells 579248f219cSDavid Howells del_timer_sync(&call->timer); 5808c3e34a4SDavid Howells 5818d94aa38SDavid Howells ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); 5828c3e34a4SDavid Howells ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); 583e653cfe4SDavid Howells ASSERTCMP(call->conn, ==, NULL); 5848c3e34a4SDavid Howells 585248f219cSDavid Howells /* Clean up the Rx/Tx buffer */ 586248f219cSDavid Howells for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) 58771f3ca40SDavid Howells rxrpc_free_skb(call->rxtx_buffer[i], 58871f3ca40SDavid Howells (call->tx_phase ? rxrpc_skb_tx_cleaned : 58971f3ca40SDavid Howells rxrpc_skb_rx_cleaned)); 5908c3e34a4SDavid Howells 59171f3ca40SDavid Howells rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned); 5928c3e34a4SDavid Howells 593dee46364SDavid Howells call_rcu(&call->rcu, rxrpc_rcu_destroy_call); 5948c3e34a4SDavid Howells } 5958c3e34a4SDavid Howells 5968c3e34a4SDavid Howells /* 5972baec2c3SDavid Howells * Make sure that all calls are gone from a network namespace. To reach this 5982baec2c3SDavid Howells * point, any open UDP sockets in that namespace must have been closed, so any 5992baec2c3SDavid Howells * outstanding calls cannot be doing I/O. 6008c3e34a4SDavid Howells */ 6012baec2c3SDavid Howells void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) 6028c3e34a4SDavid Howells { 6038c3e34a4SDavid Howells struct rxrpc_call *call; 6048c3e34a4SDavid Howells 6058c3e34a4SDavid Howells _enter(""); 6068d94aa38SDavid Howells 607b1302342SDavid Howells if (!list_empty(&rxnet->calls)) { 6082baec2c3SDavid Howells write_lock(&rxnet->call_lock); 6098c3e34a4SDavid Howells 6102baec2c3SDavid Howells while (!list_empty(&rxnet->calls)) { 611b1302342SDavid Howells call = list_entry(rxnet->calls.next, 612b1302342SDavid Howells struct rxrpc_call, link); 6138c3e34a4SDavid Howells _debug("Zapping call %p", call); 6148c3e34a4SDavid Howells 615e34d4234SDavid Howells rxrpc_see_call(call); 6168c3e34a4SDavid Howells list_del_init(&call->link); 6178c3e34a4SDavid Howells 618248f219cSDavid Howells pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", 6198c3e34a4SDavid Howells call, atomic_read(&call->usage), 6208c3e34a4SDavid Howells rxrpc_call_states[call->state], 6218c3e34a4SDavid Howells call->flags, call->events); 6228c3e34a4SDavid Howells 6232baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 6248c3e34a4SDavid Howells cond_resched(); 6252baec2c3SDavid Howells write_lock(&rxnet->call_lock); 6268c3e34a4SDavid Howells } 6278c3e34a4SDavid Howells 6282baec2c3SDavid Howells write_unlock(&rxnet->call_lock); 629b1302342SDavid Howells } 630d3be4d24SDavid Howells 631d3be4d24SDavid Howells atomic_dec(&rxnet->nr_calls); 6325bb053beSLinus Torvalds wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls)); 6338c3e34a4SDavid Howells } 634