Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* RxRPC individual remote procedure call handling
0003  *
0004  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/circ_buf.h>
0013 #include <linux/spinlock_types.h>
0014 #include <net/sock.h>
0015 #include <net/af_rxrpc.h>
0016 #include "ar-internal.h"
0017 
0018 const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
0019     [RXRPC_CALL_UNINITIALISED]      = "Uninit  ",
0020     [RXRPC_CALL_CLIENT_AWAIT_CONN]      = "ClWtConn",
0021     [RXRPC_CALL_CLIENT_SEND_REQUEST]    = "ClSndReq",
0022     [RXRPC_CALL_CLIENT_AWAIT_REPLY]     = "ClAwtRpl",
0023     [RXRPC_CALL_CLIENT_RECV_REPLY]      = "ClRcvRpl",
0024     [RXRPC_CALL_SERVER_PREALLOC]        = "SvPrealc",
0025     [RXRPC_CALL_SERVER_SECURING]        = "SvSecure",
0026     [RXRPC_CALL_SERVER_RECV_REQUEST]    = "SvRcvReq",
0027     [RXRPC_CALL_SERVER_ACK_REQUEST]     = "SvAckReq",
0028     [RXRPC_CALL_SERVER_SEND_REPLY]      = "SvSndRpl",
0029     [RXRPC_CALL_SERVER_AWAIT_ACK]       = "SvAwtACK",
0030     [RXRPC_CALL_COMPLETE]           = "Complete",
0031 };
0032 
0033 const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
0034     [RXRPC_CALL_SUCCEEDED]          = "Complete",
0035     [RXRPC_CALL_REMOTELY_ABORTED]       = "RmtAbort",
0036     [RXRPC_CALL_LOCALLY_ABORTED]        = "LocAbort",
0037     [RXRPC_CALL_LOCAL_ERROR]        = "LocError",
0038     [RXRPC_CALL_NETWORK_ERROR]      = "NetError",
0039 };
0040 
0041 struct kmem_cache *rxrpc_call_jar;
0042 
0043 static struct semaphore rxrpc_call_limiter =
0044     __SEMAPHORE_INITIALIZER(rxrpc_call_limiter, 1000);
0045 static struct semaphore rxrpc_kernel_call_limiter =
0046     __SEMAPHORE_INITIALIZER(rxrpc_kernel_call_limiter, 1000);
0047 
0048 static void rxrpc_call_timer_expired(struct timer_list *t)
0049 {
0050     struct rxrpc_call *call = from_timer(call, t, timer);
0051 
0052     _enter("%d", call->debug_id);
0053 
0054     if (call->state < RXRPC_CALL_COMPLETE) {
0055         trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies);
0056         __rxrpc_queue_call(call);
0057     } else {
0058         rxrpc_put_call(call, rxrpc_call_put);
0059     }
0060 }
0061 
0062 void rxrpc_reduce_call_timer(struct rxrpc_call *call,
0063                  unsigned long expire_at,
0064                  unsigned long now,
0065                  enum rxrpc_timer_trace why)
0066 {
0067     if (rxrpc_try_get_call(call, rxrpc_call_got_timer)) {
0068         trace_rxrpc_timer(call, why, now);
0069         if (timer_reduce(&call->timer, expire_at))
0070             rxrpc_put_call(call, rxrpc_call_put_notimer);
0071     }
0072 }
0073 
0074 void rxrpc_delete_call_timer(struct rxrpc_call *call)
0075 {
0076     if (del_timer_sync(&call->timer))
0077         rxrpc_put_call(call, rxrpc_call_put_timer);
0078 }
0079 
0080 static struct lock_class_key rxrpc_call_user_mutex_lock_class_key;
0081 
0082 /*
0083  * find an extant server call
0084  * - called in process context with IRQs enabled
0085  */
0086 struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
0087                           unsigned long user_call_ID)
0088 {
0089     struct rxrpc_call *call;
0090     struct rb_node *p;
0091 
0092     _enter("%p,%lx", rx, user_call_ID);
0093 
0094     read_lock(&rx->call_lock);
0095 
0096     p = rx->calls.rb_node;
0097     while (p) {
0098         call = rb_entry(p, struct rxrpc_call, sock_node);
0099 
0100         if (user_call_ID < call->user_call_ID)
0101             p = p->rb_left;
0102         else if (user_call_ID > call->user_call_ID)
0103             p = p->rb_right;
0104         else
0105             goto found_extant_call;
0106     }
0107 
0108     read_unlock(&rx->call_lock);
0109     _leave(" = NULL");
0110     return NULL;
0111 
0112 found_extant_call:
0113     rxrpc_get_call(call, rxrpc_call_got);
0114     read_unlock(&rx->call_lock);
0115     _leave(" = %p [%d]", call, refcount_read(&call->ref));
0116     return call;
0117 }
0118 
0119 /*
0120  * allocate a new call
0121  */
0122 struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp,
0123                     unsigned int debug_id)
0124 {
0125     struct rxrpc_call *call;
0126     struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
0127 
0128     call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
0129     if (!call)
0130         return NULL;
0131 
0132     call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
0133                     sizeof(struct sk_buff *),
0134                     gfp);
0135     if (!call->rxtx_buffer)
0136         goto nomem;
0137 
0138     call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
0139     if (!call->rxtx_annotations)
0140         goto nomem_2;
0141 
0142     mutex_init(&call->user_mutex);
0143 
0144     /* Prevent lockdep reporting a deadlock false positive between the afs
0145      * filesystem and sys_sendmsg() via the mmap sem.
0146      */
0147     if (rx->sk.sk_kern_sock)
0148         lockdep_set_class(&call->user_mutex,
0149                   &rxrpc_call_user_mutex_lock_class_key);
0150 
0151     timer_setup(&call->timer, rxrpc_call_timer_expired, 0);
0152     INIT_WORK(&call->processor, &rxrpc_process_call);
0153     INIT_LIST_HEAD(&call->link);
0154     INIT_LIST_HEAD(&call->chan_wait_link);
0155     INIT_LIST_HEAD(&call->accept_link);
0156     INIT_LIST_HEAD(&call->recvmsg_link);
0157     INIT_LIST_HEAD(&call->sock_link);
0158     init_waitqueue_head(&call->waitq);
0159     spin_lock_init(&call->lock);
0160     spin_lock_init(&call->notify_lock);
0161     spin_lock_init(&call->input_lock);
0162     rwlock_init(&call->state_lock);
0163     refcount_set(&call->ref, 1);
0164     call->debug_id = debug_id;
0165     call->tx_total_len = -1;
0166     call->next_rx_timo = 20 * HZ;
0167     call->next_req_timo = 1 * HZ;
0168 
0169     memset(&call->sock_node, 0xed, sizeof(call->sock_node));
0170 
0171     /* Leave space in the ring to handle a maxed-out jumbo packet */
0172     call->rx_winsize = rxrpc_rx_window_size;
0173     call->tx_winsize = 16;
0174     call->rx_expect_next = 1;
0175 
0176     call->cong_cwnd = 2;
0177     call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
0178 
0179     call->rxnet = rxnet;
0180     call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK;
0181     atomic_inc(&rxnet->nr_calls);
0182     return call;
0183 
0184 nomem_2:
0185     kfree(call->rxtx_buffer);
0186 nomem:
0187     kmem_cache_free(rxrpc_call_jar, call);
0188     return NULL;
0189 }
0190 
0191 /*
0192  * Allocate a new client call.
0193  */
0194 static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
0195                           struct sockaddr_rxrpc *srx,
0196                           gfp_t gfp,
0197                           unsigned int debug_id)
0198 {
0199     struct rxrpc_call *call;
0200     ktime_t now;
0201 
0202     _enter("");
0203 
0204     call = rxrpc_alloc_call(rx, gfp, debug_id);
0205     if (!call)
0206         return ERR_PTR(-ENOMEM);
0207     call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
0208     call->service_id = srx->srx_service;
0209     call->tx_phase = true;
0210     now = ktime_get_real();
0211     call->acks_latest_ts = now;
0212     call->cong_tstamp = now;
0213 
0214     _leave(" = %p", call);
0215     return call;
0216 }
0217 
0218 /*
0219  * Initiate the call ack/resend/expiry timer.
0220  */
0221 static void rxrpc_start_call_timer(struct rxrpc_call *call)
0222 {
0223     unsigned long now = jiffies;
0224     unsigned long j = now + MAX_JIFFY_OFFSET;
0225 
0226     call->ack_at = j;
0227     call->ack_lost_at = j;
0228     call->resend_at = j;
0229     call->ping_at = j;
0230     call->expect_rx_by = j;
0231     call->expect_req_by = j;
0232     call->expect_term_by = j;
0233     call->timer.expires = now;
0234 }
0235 
0236 /*
0237  * Wait for a call slot to become available.
0238  */
0239 static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp)
0240 {
0241     struct semaphore *limiter = &rxrpc_call_limiter;
0242 
0243     if (p->kernel)
0244         limiter = &rxrpc_kernel_call_limiter;
0245     if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) {
0246         down(limiter);
0247         return limiter;
0248     }
0249     return down_interruptible(limiter) < 0 ? NULL : limiter;
0250 }
0251 
0252 /*
0253  * Release a call slot.
0254  */
0255 static void rxrpc_put_call_slot(struct rxrpc_call *call)
0256 {
0257     struct semaphore *limiter = &rxrpc_call_limiter;
0258 
0259     if (test_bit(RXRPC_CALL_KERNEL, &call->flags))
0260         limiter = &rxrpc_kernel_call_limiter;
0261     up(limiter);
0262 }
0263 
0264 /*
0265  * Set up a call for the given parameters.
0266  * - Called with the socket lock held, which it must release.
0267  * - If it returns a call, the call's lock will need releasing by the caller.
0268  */
0269 struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
0270                      struct rxrpc_conn_parameters *cp,
0271                      struct sockaddr_rxrpc *srx,
0272                      struct rxrpc_call_params *p,
0273                      gfp_t gfp,
0274                      unsigned int debug_id)
0275     __releases(&rx->sk.sk_lock.slock)
0276     __acquires(&call->user_mutex)
0277 {
0278     struct rxrpc_call *call, *xcall;
0279     struct rxrpc_net *rxnet;
0280     struct semaphore *limiter;
0281     struct rb_node *parent, **pp;
0282     const void *here = __builtin_return_address(0);
0283     int ret;
0284 
0285     _enter("%p,%lx", rx, p->user_call_ID);
0286 
0287     limiter = rxrpc_get_call_slot(p, gfp);
0288     if (!limiter) {
0289         release_sock(&rx->sk);
0290         return ERR_PTR(-ERESTARTSYS);
0291     }
0292 
0293     call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id);
0294     if (IS_ERR(call)) {
0295         release_sock(&rx->sk);
0296         up(limiter);
0297         _leave(" = %ld", PTR_ERR(call));
0298         return call;
0299     }
0300 
0301     call->interruptibility = p->interruptibility;
0302     call->tx_total_len = p->tx_total_len;
0303     trace_rxrpc_call(call->debug_id, rxrpc_call_new_client,
0304              refcount_read(&call->ref),
0305              here, (const void *)p->user_call_ID);
0306     if (p->kernel)
0307         __set_bit(RXRPC_CALL_KERNEL, &call->flags);
0308 
0309     /* We need to protect a partially set up call against the user as we
0310      * will be acting outside the socket lock.
0311      */
0312     mutex_lock(&call->user_mutex);
0313 
0314     /* Publish the call, even though it is incompletely set up as yet */
0315     write_lock(&rx->call_lock);
0316 
0317     pp = &rx->calls.rb_node;
0318     parent = NULL;
0319     while (*pp) {
0320         parent = *pp;
0321         xcall = rb_entry(parent, struct rxrpc_call, sock_node);
0322 
0323         if (p->user_call_ID < xcall->user_call_ID)
0324             pp = &(*pp)->rb_left;
0325         else if (p->user_call_ID > xcall->user_call_ID)
0326             pp = &(*pp)->rb_right;
0327         else
0328             goto error_dup_user_ID;
0329     }
0330 
0331     rcu_assign_pointer(call->socket, rx);
0332     call->user_call_ID = p->user_call_ID;
0333     __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
0334     rxrpc_get_call(call, rxrpc_call_got_userid);
0335     rb_link_node(&call->sock_node, parent, pp);
0336     rb_insert_color(&call->sock_node, &rx->calls);
0337     list_add(&call->sock_link, &rx->sock_calls);
0338 
0339     write_unlock(&rx->call_lock);
0340 
0341     rxnet = call->rxnet;
0342     spin_lock_bh(&rxnet->call_lock);
0343     list_add_tail_rcu(&call->link, &rxnet->calls);
0344     spin_unlock_bh(&rxnet->call_lock);
0345 
0346     /* From this point on, the call is protected by its own lock. */
0347     release_sock(&rx->sk);
0348 
0349     /* Set up or get a connection record and set the protocol parameters,
0350      * including channel number and call ID.
0351      */
0352     ret = rxrpc_connect_call(rx, call, cp, srx, gfp);
0353     if (ret < 0)
0354         goto error_attached_to_socket;
0355 
0356     trace_rxrpc_call(call->debug_id, rxrpc_call_connected,
0357              refcount_read(&call->ref), here, NULL);
0358 
0359     rxrpc_start_call_timer(call);
0360 
0361     _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
0362 
0363     _leave(" = %p [new]", call);
0364     return call;
0365 
0366     /* We unexpectedly found the user ID in the list after taking
0367      * the call_lock.  This shouldn't happen unless the user races
0368      * with itself and tries to add the same user ID twice at the
0369      * same time in different threads.
0370      */
0371 error_dup_user_ID:
0372     write_unlock(&rx->call_lock);
0373     release_sock(&rx->sk);
0374     __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
0375                     RX_CALL_DEAD, -EEXIST);
0376     trace_rxrpc_call(call->debug_id, rxrpc_call_error,
0377              refcount_read(&call->ref), here, ERR_PTR(-EEXIST));
0378     rxrpc_release_call(rx, call);
0379     mutex_unlock(&call->user_mutex);
0380     rxrpc_put_call(call, rxrpc_call_put);
0381     _leave(" = -EEXIST");
0382     return ERR_PTR(-EEXIST);
0383 
0384     /* We got an error, but the call is attached to the socket and is in
0385      * need of release.  However, we might now race with recvmsg() when
0386      * completing the call queues it.  Return 0 from sys_sendmsg() and
0387      * leave the error to recvmsg() to deal with.
0388      */
0389 error_attached_to_socket:
0390     trace_rxrpc_call(call->debug_id, rxrpc_call_error,
0391              refcount_read(&call->ref), here, ERR_PTR(ret));
0392     set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
0393     __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
0394                     RX_CALL_DEAD, ret);
0395     _leave(" = c=%08x [err]", call->debug_id);
0396     return call;
0397 }
0398 
0399 /*
0400  * Set up an incoming call.  call->conn points to the connection.
0401  * This is called in BH context and isn't allowed to fail.
0402  */
0403 void rxrpc_incoming_call(struct rxrpc_sock *rx,
0404              struct rxrpc_call *call,
0405              struct sk_buff *skb)
0406 {
0407     struct rxrpc_connection *conn = call->conn;
0408     struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
0409     u32 chan;
0410 
0411     _enter(",%d", call->conn->debug_id);
0412 
0413     rcu_assign_pointer(call->socket, rx);
0414     call->call_id       = sp->hdr.callNumber;
0415     call->service_id    = sp->hdr.serviceId;
0416     call->cid       = sp->hdr.cid;
0417     call->state     = RXRPC_CALL_SERVER_SECURING;
0418     call->cong_tstamp   = skb->tstamp;
0419 
0420     /* Set the channel for this call.  We don't get channel_lock as we're
0421      * only defending against the data_ready handler (which we're called
0422      * from) and the RESPONSE packet parser (which is only really
0423      * interested in call_counter and can cope with a disagreement with the
0424      * call pointer).
0425      */
0426     chan = sp->hdr.cid & RXRPC_CHANNELMASK;
0427     conn->channels[chan].call_counter = call->call_id;
0428     conn->channels[chan].call_id = call->call_id;
0429     rcu_assign_pointer(conn->channels[chan].call, call);
0430 
0431     spin_lock(&conn->params.peer->lock);
0432     hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets);
0433     spin_unlock(&conn->params.peer->lock);
0434 
0435     _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
0436 
0437     rxrpc_start_call_timer(call);
0438     _leave("");
0439 }
0440 
0441 /*
0442  * Queue a call's work processor, getting a ref to pass to the work queue.
0443  */
0444 bool rxrpc_queue_call(struct rxrpc_call *call)
0445 {
0446     const void *here = __builtin_return_address(0);
0447     int n;
0448 
0449     if (!__refcount_inc_not_zero(&call->ref, &n))
0450         return false;
0451     if (rxrpc_queue_work(&call->processor))
0452         trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1,
0453                  here, NULL);
0454     else
0455         rxrpc_put_call(call, rxrpc_call_put_noqueue);
0456     return true;
0457 }
0458 
0459 /*
0460  * Queue a call's work processor, passing the callers ref to the work queue.
0461  */
0462 bool __rxrpc_queue_call(struct rxrpc_call *call)
0463 {
0464     const void *here = __builtin_return_address(0);
0465     int n = refcount_read(&call->ref);
0466     ASSERTCMP(n, >=, 1);
0467     if (rxrpc_queue_work(&call->processor))
0468         trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n,
0469                  here, NULL);
0470     else
0471         rxrpc_put_call(call, rxrpc_call_put_noqueue);
0472     return true;
0473 }
0474 
0475 /*
0476  * Note the re-emergence of a call.
0477  */
0478 void rxrpc_see_call(struct rxrpc_call *call)
0479 {
0480     const void *here = __builtin_return_address(0);
0481     if (call) {
0482         int n = refcount_read(&call->ref);
0483 
0484         trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n,
0485                  here, NULL);
0486     }
0487 }
0488 
0489 bool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
0490 {
0491     const void *here = __builtin_return_address(0);
0492     int n;
0493 
0494     if (!__refcount_inc_not_zero(&call->ref, &n))
0495         return false;
0496     trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL);
0497     return true;
0498 }
0499 
0500 /*
0501  * Note the addition of a ref on a call.
0502  */
0503 void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
0504 {
0505     const void *here = __builtin_return_address(0);
0506     int n;
0507 
0508     __refcount_inc(&call->ref, &n);
0509     trace_rxrpc_call(call->debug_id, op, n + 1, here, NULL);
0510 }
0511 
0512 /*
0513  * Clean up the RxTx skb ring.
0514  */
0515 static void rxrpc_cleanup_ring(struct rxrpc_call *call)
0516 {
0517     int i;
0518 
0519     for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
0520         rxrpc_free_skb(call->rxtx_buffer[i], rxrpc_skb_cleaned);
0521         call->rxtx_buffer[i] = NULL;
0522     }
0523 }
0524 
0525 /*
0526  * Detach a call from its owning socket.
0527  */
0528 void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
0529 {
0530     const void *here = __builtin_return_address(0);
0531     struct rxrpc_connection *conn = call->conn;
0532     bool put = false;
0533 
0534     _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref));
0535 
0536     trace_rxrpc_call(call->debug_id, rxrpc_call_release,
0537              refcount_read(&call->ref),
0538              here, (const void *)call->flags);
0539 
0540     ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
0541 
0542     spin_lock_bh(&call->lock);
0543     if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
0544         BUG();
0545     spin_unlock_bh(&call->lock);
0546 
0547     rxrpc_put_call_slot(call);
0548     rxrpc_delete_call_timer(call);
0549 
0550     /* Make sure we don't get any more notifications */
0551     write_lock_bh(&rx->recvmsg_lock);
0552 
0553     if (!list_empty(&call->recvmsg_link)) {
0554         _debug("unlinking once-pending call %p { e=%lx f=%lx }",
0555                call, call->events, call->flags);
0556         list_del(&call->recvmsg_link);
0557         put = true;
0558     }
0559 
0560     /* list_empty() must return false in rxrpc_notify_socket() */
0561     call->recvmsg_link.next = NULL;
0562     call->recvmsg_link.prev = NULL;
0563 
0564     write_unlock_bh(&rx->recvmsg_lock);
0565     if (put)
0566         rxrpc_put_call(call, rxrpc_call_put);
0567 
0568     write_lock(&rx->call_lock);
0569 
0570     if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
0571         rb_erase(&call->sock_node, &rx->calls);
0572         memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
0573         rxrpc_put_call(call, rxrpc_call_put_userid);
0574     }
0575 
0576     list_del(&call->sock_link);
0577     write_unlock(&rx->call_lock);
0578 
0579     _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
0580 
0581     if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
0582         rxrpc_disconnect_call(call);
0583     if (call->security)
0584         call->security->free_call_crypto(call);
0585     _leave("");
0586 }
0587 
0588 /*
0589  * release all the calls associated with a socket
0590  */
0591 void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
0592 {
0593     struct rxrpc_call *call;
0594 
0595     _enter("%p", rx);
0596 
0597     while (!list_empty(&rx->to_be_accepted)) {
0598         call = list_entry(rx->to_be_accepted.next,
0599                   struct rxrpc_call, accept_link);
0600         list_del(&call->accept_link);
0601         rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
0602         rxrpc_put_call(call, rxrpc_call_put);
0603     }
0604 
0605     while (!list_empty(&rx->sock_calls)) {
0606         call = list_entry(rx->sock_calls.next,
0607                   struct rxrpc_call, sock_link);
0608         rxrpc_get_call(call, rxrpc_call_got);
0609         rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
0610         rxrpc_send_abort_packet(call);
0611         rxrpc_release_call(rx, call);
0612         rxrpc_put_call(call, rxrpc_call_put);
0613     }
0614 
0615     _leave("");
0616 }
0617 
0618 /*
0619  * release a call
0620  */
0621 void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
0622 {
0623     struct rxrpc_net *rxnet = call->rxnet;
0624     const void *here = __builtin_return_address(0);
0625     unsigned int debug_id = call->debug_id;
0626     bool dead;
0627     int n;
0628 
0629     ASSERT(call != NULL);
0630 
0631     dead = __refcount_dec_and_test(&call->ref, &n);
0632     trace_rxrpc_call(debug_id, op, n, here, NULL);
0633     if (dead) {
0634         _debug("call %d dead", call->debug_id);
0635         ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
0636 
0637         if (!list_empty(&call->link)) {
0638             spin_lock_bh(&rxnet->call_lock);
0639             list_del_init(&call->link);
0640             spin_unlock_bh(&rxnet->call_lock);
0641         }
0642 
0643         rxrpc_cleanup_call(call);
0644     }
0645 }
0646 
0647 /*
0648  * Final call destruction - but must be done in process context.
0649  */
0650 static void rxrpc_destroy_call(struct work_struct *work)
0651 {
0652     struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor);
0653     struct rxrpc_net *rxnet = call->rxnet;
0654 
0655     rxrpc_delete_call_timer(call);
0656 
0657     rxrpc_put_connection(call->conn);
0658     rxrpc_put_peer(call->peer);
0659     kfree(call->rxtx_buffer);
0660     kfree(call->rxtx_annotations);
0661     kmem_cache_free(rxrpc_call_jar, call);
0662     if (atomic_dec_and_test(&rxnet->nr_calls))
0663         wake_up_var(&rxnet->nr_calls);
0664 }
0665 
0666 /*
0667  * Final call destruction under RCU.
0668  */
0669 static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
0670 {
0671     struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
0672 
0673     if (in_softirq()) {
0674         INIT_WORK(&call->processor, rxrpc_destroy_call);
0675         if (!rxrpc_queue_work(&call->processor))
0676             BUG();
0677     } else {
0678         rxrpc_destroy_call(&call->processor);
0679     }
0680 }
0681 
0682 /*
0683  * clean up a call
0684  */
0685 void rxrpc_cleanup_call(struct rxrpc_call *call)
0686 {
0687     _net("DESTROY CALL %d", call->debug_id);
0688 
0689     memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
0690 
0691     ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
0692     ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
0693 
0694     rxrpc_cleanup_ring(call);
0695     rxrpc_free_skb(call->tx_pending, rxrpc_skb_cleaned);
0696 
0697     call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
0698 }
0699 
0700 /*
0701  * Make sure that all calls are gone from a network namespace.  To reach this
0702  * point, any open UDP sockets in that namespace must have been closed, so any
0703  * outstanding calls cannot be doing I/O.
0704  */
0705 void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
0706 {
0707     struct rxrpc_call *call;
0708 
0709     _enter("");
0710 
0711     if (!list_empty(&rxnet->calls)) {
0712         spin_lock_bh(&rxnet->call_lock);
0713 
0714         while (!list_empty(&rxnet->calls)) {
0715             call = list_entry(rxnet->calls.next,
0716                       struct rxrpc_call, link);
0717             _debug("Zapping call %p", call);
0718 
0719             rxrpc_see_call(call);
0720             list_del_init(&call->link);
0721 
0722             pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
0723                    call, refcount_read(&call->ref),
0724                    rxrpc_call_states[call->state],
0725                    call->flags, call->events);
0726 
0727             spin_unlock_bh(&rxnet->call_lock);
0728             cond_resched();
0729             spin_lock_bh(&rxnet->call_lock);
0730         }
0731 
0732         spin_unlock_bh(&rxnet->call_lock);
0733     }
0734 
0735     atomic_dec(&rxnet->nr_calls);
0736     wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
0737 }