blob: f23432591a0fb1b4976b265d915265ed9c0170f7 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC individual remote procedure call handling
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070015#include <linux/module.h>
16#include <linux/circ_buf.h>
Tim Smith77276402014-03-03 23:04:45 +000017#include <linux/spinlock_types.h>
David Howells17926a72007-04-26 15:48:28 -070018#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
David Howells5873c082014-02-07 18:58:44 +000022/*
23 * Maximum lifetime of a call (in jiffies).
24 */
David Howellsdad8aff2016-03-09 23:22:56 +000025unsigned int rxrpc_max_call_lifetime = 60 * HZ;
David Howells5873c082014-02-07 18:58:44 +000026
27/*
28 * Time till dead call expires after last use (in jiffies).
29 */
David Howellsdad8aff2016-03-09 23:22:56 +000030unsigned int rxrpc_dead_call_expiry = 2 * HZ;
David Howells5873c082014-02-07 18:58:44 +000031
David Howells5b8848d2016-03-04 15:53:46 +000032const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
David Howells999b69f2016-06-17 15:42:35 +010033 [RXRPC_CALL_UNINITIALISED] = "Uninit",
34 [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn",
David Howells1f8481d2007-05-22 16:14:24 -070035 [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq",
36 [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl",
37 [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl",
38 [RXRPC_CALL_CLIENT_FINAL_ACK] = "ClFnlACK",
39 [RXRPC_CALL_SERVER_SECURING] = "SvSecure",
40 [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept",
41 [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq",
42 [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq",
43 [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl",
44 [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK",
45 [RXRPC_CALL_COMPLETE] = "Complete",
46 [RXRPC_CALL_SERVER_BUSY] = "SvBusy ",
47 [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort",
48 [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort",
49 [RXRPC_CALL_NETWORK_ERROR] = "NetError",
50 [RXRPC_CALL_DEAD] = "Dead ",
51};
52
David Howells17926a72007-04-26 15:48:28 -070053struct kmem_cache *rxrpc_call_jar;
54LIST_HEAD(rxrpc_calls);
55DEFINE_RWLOCK(rxrpc_call_lock);
David Howells17926a72007-04-26 15:48:28 -070056
57static void rxrpc_destroy_call(struct work_struct *work);
58static void rxrpc_call_life_expired(unsigned long _call);
59static void rxrpc_dead_call_expired(unsigned long _call);
60static void rxrpc_ack_time_expired(unsigned long _call);
61static void rxrpc_resend_time_expired(unsigned long _call);
62
63/*
David Howells2341e072016-06-09 23:02:51 +010064 * find an extant server call
65 * - called in process context with IRQs enabled
66 */
67struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
68 unsigned long user_call_ID)
69{
70 struct rxrpc_call *call;
71 struct rb_node *p;
72
73 _enter("%p,%lx", rx, user_call_ID);
74
75 read_lock(&rx->call_lock);
76
77 p = rx->calls.rb_node;
78 while (p) {
79 call = rb_entry(p, struct rxrpc_call, sock_node);
80
81 if (user_call_ID < call->user_call_ID)
82 p = p->rb_left;
83 else if (user_call_ID > call->user_call_ID)
84 p = p->rb_right;
85 else
86 goto found_extant_call;
87 }
88
89 read_unlock(&rx->call_lock);
90 _leave(" = NULL");
91 return NULL;
92
93found_extant_call:
94 rxrpc_get_call(call);
95 read_unlock(&rx->call_lock);
96 _leave(" = %p [%d]", call, atomic_read(&call->usage));
97 return call;
98}
99
100/*
David Howells17926a72007-04-26 15:48:28 -0700101 * allocate a new call
102 */
103static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
104{
105 struct rxrpc_call *call;
106
107 call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
108 if (!call)
109 return NULL;
110
111 call->acks_winsz = 16;
112 call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
113 gfp);
114 if (!call->acks_window) {
115 kmem_cache_free(rxrpc_call_jar, call);
116 return NULL;
117 }
118
119 setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
120 (unsigned long) call);
121 setup_timer(&call->deadspan, &rxrpc_dead_call_expired,
122 (unsigned long) call);
123 setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
124 (unsigned long) call);
125 setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
126 (unsigned long) call);
127 INIT_WORK(&call->destroyer, &rxrpc_destroy_call);
128 INIT_WORK(&call->processor, &rxrpc_process_call);
David Howells999b69f2016-06-17 15:42:35 +0100129 INIT_LIST_HEAD(&call->link);
David Howells17926a72007-04-26 15:48:28 -0700130 INIT_LIST_HEAD(&call->accept_link);
131 skb_queue_head_init(&call->rx_queue);
132 skb_queue_head_init(&call->rx_oos_queue);
133 init_waitqueue_head(&call->tx_waitq);
134 spin_lock_init(&call->lock);
135 rwlock_init(&call->state_lock);
136 atomic_set(&call->usage, 1);
137 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells17926a72007-04-26 15:48:28 -0700138
139 memset(&call->sock_node, 0xed, sizeof(call->sock_node));
140
141 call->rx_data_expect = 1;
142 call->rx_data_eaten = 0;
143 call->rx_first_oos = 0;
David Howells817913d2014-02-07 18:10:30 +0000144 call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
David Howells17926a72007-04-26 15:48:28 -0700145 call->creation_jif = jiffies;
146 return call;
147}
148
149/*
David Howells999b69f2016-06-17 15:42:35 +0100150 * Allocate a new client call.
David Howells17926a72007-04-26 15:48:28 -0700151 */
David Howellsaa390bb2016-06-17 10:06:56 +0100152static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
153 struct sockaddr_rxrpc *srx,
154 gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700155{
156 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700157
158 _enter("");
159
David Howells999b69f2016-06-17 15:42:35 +0100160 ASSERT(rx->local != NULL);
David Howells17926a72007-04-26 15:48:28 -0700161
162 call = rxrpc_alloc_call(gfp);
163 if (!call)
164 return ERR_PTR(-ENOMEM);
David Howells999b69f2016-06-17 15:42:35 +0100165 call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
David Howells17926a72007-04-26 15:48:28 -0700166
167 sock_hold(&rx->sk);
168 call->socket = rx;
169 call->rx_data_post = 1;
David Howells999b69f2016-06-17 15:42:35 +0100170 call->service_id = srx->srx_service;
David Howells999b69f2016-06-17 15:42:35 +0100171
172 _leave(" = %p", call);
173 return call;
174}
175
176/*
177 * Begin client call.
178 */
179static int rxrpc_begin_client_call(struct rxrpc_call *call,
180 struct rxrpc_conn_parameters *cp,
David Howells999b69f2016-06-17 15:42:35 +0100181 struct sockaddr_rxrpc *srx,
182 gfp_t gfp)
183{
184 int ret;
185
186 /* Set up or get a connection record and set the protocol parameters,
187 * including channel number and call ID.
188 */
David Howellsaa390bb2016-06-17 10:06:56 +0100189 ret = rxrpc_connect_call(call, cp, srx, gfp);
David Howells999b69f2016-06-17 15:42:35 +0100190 if (ret < 0)
191 return ret;
192
193 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
194
David Howells85f32272016-04-04 14:00:36 +0100195 spin_lock(&call->conn->params.peer->lock);
196 hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
197 spin_unlock(&call->conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700198
David Howells5873c082014-02-07 18:58:44 +0000199 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700200 add_timer(&call->lifetimer);
David Howells999b69f2016-06-17 15:42:35 +0100201 return 0;
David Howells17926a72007-04-26 15:48:28 -0700202}
203
204/*
205 * set up a call for the given data
206 * - called in process context with IRQs enabled
207 */
David Howells2341e072016-06-09 23:02:51 +0100208struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100209 struct rxrpc_conn_parameters *cp,
David Howells999b69f2016-06-17 15:42:35 +0100210 struct sockaddr_rxrpc *srx,
David Howells17926a72007-04-26 15:48:28 -0700211 unsigned long user_call_ID,
David Howells17926a72007-04-26 15:48:28 -0700212 gfp_t gfp)
213{
David Howells2341e072016-06-09 23:02:51 +0100214 struct rxrpc_call *call, *xcall;
215 struct rb_node *parent, **pp;
David Howells999b69f2016-06-17 15:42:35 +0100216 int ret;
David Howells17926a72007-04-26 15:48:28 -0700217
David Howells999b69f2016-06-17 15:42:35 +0100218 _enter("%p,%lx", rx, user_call_ID);
David Howells17926a72007-04-26 15:48:28 -0700219
David Howellsaa390bb2016-06-17 10:06:56 +0100220 call = rxrpc_alloc_client_call(rx, srx, gfp);
David Howells2341e072016-06-09 23:02:51 +0100221 if (IS_ERR(call)) {
222 _leave(" = %ld", PTR_ERR(call));
223 return call;
David Howells17926a72007-04-26 15:48:28 -0700224 }
225
David Howells999b69f2016-06-17 15:42:35 +0100226 /* Publish the call, even though it is incompletely set up as yet */
David Howells2341e072016-06-09 23:02:51 +0100227 call->user_call_ID = user_call_ID;
228 __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
David Howells17926a72007-04-26 15:48:28 -0700229
230 write_lock(&rx->call_lock);
231
232 pp = &rx->calls.rb_node;
233 parent = NULL;
234 while (*pp) {
235 parent = *pp;
David Howells2341e072016-06-09 23:02:51 +0100236 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
David Howells17926a72007-04-26 15:48:28 -0700237
David Howells2341e072016-06-09 23:02:51 +0100238 if (user_call_ID < xcall->user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700239 pp = &(*pp)->rb_left;
David Howells2341e072016-06-09 23:02:51 +0100240 else if (user_call_ID > xcall->user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700241 pp = &(*pp)->rb_right;
242 else
David Howells2341e072016-06-09 23:02:51 +0100243 goto found_user_ID_now_present;
David Howells17926a72007-04-26 15:48:28 -0700244 }
245
David Howells17926a72007-04-26 15:48:28 -0700246 rxrpc_get_call(call);
247
248 rb_link_node(&call->sock_node, parent, pp);
249 rb_insert_color(&call->sock_node, &rx->calls);
250 write_unlock(&rx->call_lock);
251
252 write_lock_bh(&rxrpc_call_lock);
253 list_add_tail(&call->link, &rxrpc_calls);
254 write_unlock_bh(&rxrpc_call_lock);
255
David Howellsaa390bb2016-06-17 10:06:56 +0100256 ret = rxrpc_begin_client_call(call, cp, srx, gfp);
David Howells999b69f2016-06-17 15:42:35 +0100257 if (ret < 0)
258 goto error;
259
David Howells17926a72007-04-26 15:48:28 -0700260 _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
261
262 _leave(" = %p [new]", call);
263 return call;
264
David Howells999b69f2016-06-17 15:42:35 +0100265error:
266 write_lock(&rx->call_lock);
267 rb_erase(&call->sock_node, &rx->calls);
268 write_unlock(&rx->call_lock);
269 rxrpc_put_call(call);
270
271 write_lock_bh(&rxrpc_call_lock);
David Howellsd1e858c2016-04-04 14:00:39 +0100272 list_del_init(&call->link);
David Howells999b69f2016-06-17 15:42:35 +0100273 write_unlock_bh(&rxrpc_call_lock);
274
David Howells17b963e2016-08-08 13:06:41 +0100275 set_bit(RXRPC_CALL_RELEASED, &call->flags);
David Howellsd1e858c2016-04-04 14:00:39 +0100276 call->state = RXRPC_CALL_DEAD;
David Howells999b69f2016-06-17 15:42:35 +0100277 rxrpc_put_call(call);
278 _leave(" = %d", ret);
279 return ERR_PTR(ret);
280
David Howells2341e072016-06-09 23:02:51 +0100281 /* We unexpectedly found the user ID in the list after taking
282 * the call_lock. This shouldn't happen unless the user races
283 * with itself and tries to add the same user ID twice at the
284 * same time in different threads.
285 */
286found_user_ID_now_present:
David Howells17926a72007-04-26 15:48:28 -0700287 write_unlock(&rx->call_lock);
David Howells17b963e2016-08-08 13:06:41 +0100288 set_bit(RXRPC_CALL_RELEASED, &call->flags);
David Howellsd1e858c2016-04-04 14:00:39 +0100289 call->state = RXRPC_CALL_DEAD;
David Howells2341e072016-06-09 23:02:51 +0100290 rxrpc_put_call(call);
291 _leave(" = -EEXIST [%p]", call);
292 return ERR_PTR(-EEXIST);
David Howells17926a72007-04-26 15:48:28 -0700293}
294
295/*
296 * set up an incoming call
297 * - called in process context with IRQs enabled
298 */
299struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
300 struct rxrpc_connection *conn,
David Howells42886ff2016-06-16 13:31:07 +0100301 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700302{
David Howells42886ff2016-06-16 13:31:07 +0100303 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700304 struct rxrpc_call *call, *candidate;
David Howellsa1399f82016-06-27 14:39:44 +0100305 u32 call_id, chan;
David Howells17926a72007-04-26 15:48:28 -0700306
David Howells843099c2016-04-07 17:23:37 +0100307 _enter(",%d", conn->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700308
309 ASSERT(rx != NULL);
310
David Howells843099c2016-04-07 17:23:37 +0100311 candidate = rxrpc_alloc_call(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700312 if (!candidate)
313 return ERR_PTR(-EBUSY);
314
David Howellsa1399f82016-06-27 14:39:44 +0100315 chan = sp->hdr.cid & RXRPC_CHANNELMASK;
David Howells42886ff2016-06-16 13:31:07 +0100316 candidate->socket = rx;
317 candidate->conn = conn;
David Howellsdf5d8bf2016-08-24 14:31:43 +0100318 candidate->peer = conn->params.peer;
David Howells42886ff2016-06-16 13:31:07 +0100319 candidate->cid = sp->hdr.cid;
320 candidate->call_id = sp->hdr.callNumber;
David Howells42886ff2016-06-16 13:31:07 +0100321 candidate->rx_data_post = 0;
322 candidate->state = RXRPC_CALL_SERVER_ACCEPTING;
David Howellsdabe5a72016-08-23 15:27:24 +0100323 candidate->flags |= (1 << RXRPC_CALL_IS_SERVICE);
David Howells17926a72007-04-26 15:48:28 -0700324 if (conn->security_ix > 0)
325 candidate->state = RXRPC_CALL_SERVER_SECURING;
326
David Howellsa1399f82016-06-27 14:39:44 +0100327 spin_lock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700328
329 /* set the channel for this call */
David Howellsa1399f82016-06-27 14:39:44 +0100330 call = rcu_dereference_protected(conn->channels[chan].call,
331 lockdep_is_held(&conn->channel_lock));
332
David Howells01a90a42016-08-23 15:27:24 +0100333 _debug("channel[%u] is %p", candidate->cid & RXRPC_CHANNELMASK, call);
David Howells42886ff2016-06-16 13:31:07 +0100334 if (call && call->call_id == sp->hdr.callNumber) {
David Howells17926a72007-04-26 15:48:28 -0700335 /* already set; must've been a duplicate packet */
336 _debug("extant call [%d]", call->state);
337 ASSERTCMP(call->conn, ==, conn);
338
339 read_lock(&call->state_lock);
340 switch (call->state) {
341 case RXRPC_CALL_LOCALLY_ABORTED:
David Howells4c198ad2016-03-04 15:53:46 +0000342 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700343 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700344 case RXRPC_CALL_REMOTELY_ABORTED:
345 read_unlock(&call->state_lock);
346 goto aborted_call;
347 default:
348 rxrpc_get_call(call);
349 read_unlock(&call->state_lock);
350 goto extant_call;
351 }
352 }
353
354 if (call) {
355 /* it seems the channel is still in use from the previous call
356 * - ditch the old binding if its call is now complete */
357 _debug("CALL: %u { %s }",
358 call->debug_id, rxrpc_call_states[call->state]);
359
360 if (call->state >= RXRPC_CALL_COMPLETE) {
David Howellsa1399f82016-06-27 14:39:44 +0100361 __rxrpc_disconnect_call(call);
David Howells17926a72007-04-26 15:48:28 -0700362 } else {
David Howellsa1399f82016-06-27 14:39:44 +0100363 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700364 kmem_cache_free(rxrpc_call_jar, candidate);
365 _leave(" = -EBUSY");
366 return ERR_PTR(-EBUSY);
367 }
368 }
369
370 /* check the call number isn't duplicate */
371 _debug("check dup");
David Howells42886ff2016-06-16 13:31:07 +0100372 call_id = sp->hdr.callNumber;
David Howells17926a72007-04-26 15:48:28 -0700373
David Howellsa1399f82016-06-27 14:39:44 +0100374 /* We just ignore calls prior to the current call ID. Terminated calls
375 * are handled via the connection.
376 */
377 if (call_id <= conn->channels[chan].call_counter)
378 goto old_call; /* TODO: Just drop packet */
David Howells17926a72007-04-26 15:48:28 -0700379
380 /* make the call available */
381 _debug("new call");
382 call = candidate;
383 candidate = NULL;
David Howellsa1399f82016-06-27 14:39:44 +0100384 conn->channels[chan].call_counter = call_id;
385 rcu_assign_pointer(conn->channels[chan].call, call);
David Howells17926a72007-04-26 15:48:28 -0700386 sock_hold(&rx->sk);
David Howells5627cc82016-04-04 14:00:38 +0100387 rxrpc_get_connection(conn);
David Howellsdf5d8bf2016-08-24 14:31:43 +0100388 rxrpc_get_peer(call->peer);
David Howellsa1399f82016-06-27 14:39:44 +0100389 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700390
David Howells85f32272016-04-04 14:00:36 +0100391 spin_lock(&conn->params.peer->lock);
392 hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
393 spin_unlock(&conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700394
395 write_lock_bh(&rxrpc_call_lock);
396 list_add_tail(&call->link, &rxrpc_calls);
397 write_unlock_bh(&rxrpc_call_lock);
398
David Howells19ffa012016-04-04 14:00:36 +0100399 call->service_id = conn->params.service_id;
Tim Smith77276402014-03-03 23:04:45 +0000400
David Howells17926a72007-04-26 15:48:28 -0700401 _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
402
David Howells5873c082014-02-07 18:58:44 +0000403 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700404 add_timer(&call->lifetimer);
405 _leave(" = %p {%d} [new]", call, call->debug_id);
406 return call;
407
408extant_call:
David Howellsa1399f82016-06-27 14:39:44 +0100409 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700410 kmem_cache_free(rxrpc_call_jar, candidate);
411 _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
412 return call;
413
414aborted_call:
David Howellsa1399f82016-06-27 14:39:44 +0100415 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700416 kmem_cache_free(rxrpc_call_jar, candidate);
417 _leave(" = -ECONNABORTED");
418 return ERR_PTR(-ECONNABORTED);
419
420old_call:
David Howellsa1399f82016-06-27 14:39:44 +0100421 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700422 kmem_cache_free(rxrpc_call_jar, candidate);
423 _leave(" = -ECONNRESET [old]");
424 return ERR_PTR(-ECONNRESET);
425}
426
427/*
David Howells17926a72007-04-26 15:48:28 -0700428 * detach a call from a socket and set up for release
429 */
430void rxrpc_release_call(struct rxrpc_call *call)
431{
David Howells651350d2007-04-26 15:50:17 -0700432 struct rxrpc_connection *conn = call->conn;
David Howells17926a72007-04-26 15:48:28 -0700433 struct rxrpc_sock *rx = call->socket;
434
435 _enter("{%d,%d,%d,%d}",
436 call->debug_id, atomic_read(&call->usage),
437 atomic_read(&call->ackr_not_idle),
438 call->rx_first_oos);
439
440 spin_lock_bh(&call->lock);
441 if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
442 BUG();
443 spin_unlock_bh(&call->lock);
444
445 /* dissociate from the socket
446 * - the socket's ref on the call is passed to the death timer
447 */
David Howells651350d2007-04-26 15:50:17 -0700448 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
David Howells17926a72007-04-26 15:48:28 -0700449
David Howellse653cfe2016-04-04 14:00:38 +0100450 spin_lock(&conn->params.peer->lock);
451 hlist_del_init(&call->error_link);
452 spin_unlock(&conn->params.peer->lock);
453
David Howells17926a72007-04-26 15:48:28 -0700454 write_lock_bh(&rx->call_lock);
455 if (!list_empty(&call->accept_link)) {
456 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
457 call, call->events, call->flags);
458 ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
459 list_del_init(&call->accept_link);
460 sk_acceptq_removed(&rx->sk);
461 } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
462 rb_erase(&call->sock_node, &rx->calls);
463 memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
464 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
465 }
466 write_unlock_bh(&rx->call_lock);
467
David Howells17926a72007-04-26 15:48:28 -0700468 /* free up the channel for reuse */
David Howellsa1399f82016-06-27 14:39:44 +0100469 write_lock_bh(&call->state_lock);
David Howells651350d2007-04-26 15:50:17 -0700470
David Howells17926a72007-04-26 15:48:28 -0700471 if (call->state < RXRPC_CALL_COMPLETE &&
472 call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
473 _debug("+++ ABORTING STATE %d +++\n", call->state);
474 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100475 call->local_abort = RX_CALL_DEAD;
David Howells17926a72007-04-26 15:48:28 -0700476 }
David Howellsa1399f82016-06-27 14:39:44 +0100477 write_unlock_bh(&call->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700478
David Howellse653cfe2016-04-04 14:00:38 +0100479 rxrpc_disconnect_call(call);
480
David Howells651350d2007-04-26 15:50:17 -0700481 /* clean up the Rx queue */
David Howells17926a72007-04-26 15:48:28 -0700482 if (!skb_queue_empty(&call->rx_queue) ||
483 !skb_queue_empty(&call->rx_oos_queue)) {
484 struct rxrpc_skb_priv *sp;
485 struct sk_buff *skb;
486
487 _debug("purge Rx queues");
488
489 spin_lock_bh(&call->lock);
490 while ((skb = skb_dequeue(&call->rx_queue)) ||
491 (skb = skb_dequeue(&call->rx_oos_queue))) {
David Howells17926a72007-04-26 15:48:28 -0700492 spin_unlock_bh(&call->lock);
493
Arnd Bergmann55cae7a2016-08-08 12:13:45 +0200494 sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700495 _debug("- zap %s %%%u #%u",
496 rxrpc_pkts[sp->hdr.type],
David Howells0d12f8a2016-03-04 15:53:46 +0000497 sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700498 rxrpc_free_skb(skb);
499 spin_lock_bh(&call->lock);
500 }
501 spin_unlock_bh(&call->lock);
502
503 ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
504 }
505
506 del_timer_sync(&call->resend_timer);
507 del_timer_sync(&call->ack_timer);
508 del_timer_sync(&call->lifetimer);
David Howells5873c082014-02-07 18:58:44 +0000509 call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
David Howells17926a72007-04-26 15:48:28 -0700510 add_timer(&call->deadspan);
511
512 _leave("");
513}
514
515/*
516 * handle a dead call being ready for reaping
517 */
518static void rxrpc_dead_call_expired(unsigned long _call)
519{
520 struct rxrpc_call *call = (struct rxrpc_call *) _call;
521
522 _enter("{%d}", call->debug_id);
523
524 write_lock_bh(&call->state_lock);
525 call->state = RXRPC_CALL_DEAD;
526 write_unlock_bh(&call->state_lock);
527 rxrpc_put_call(call);
528}
529
530/*
531 * mark a call as to be released, aborting it if it's still in progress
532 * - called with softirqs disabled
533 */
534static void rxrpc_mark_call_released(struct rxrpc_call *call)
535{
536 bool sched;
537
538 write_lock(&call->state_lock);
539 if (call->state < RXRPC_CALL_DEAD) {
540 sched = false;
541 if (call->state < RXRPC_CALL_COMPLETE) {
542 _debug("abort call %p", call);
543 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100544 call->local_abort = RX_CALL_DEAD;
David Howells4c198ad2016-03-04 15:53:46 +0000545 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700546 sched = true;
547 }
David Howells4c198ad2016-03-04 15:53:46 +0000548 if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700549 sched = true;
550 if (sched)
David Howells651350d2007-04-26 15:50:17 -0700551 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700552 }
553 write_unlock(&call->state_lock);
554}
555
556/*
557 * release all the calls associated with a socket
558 */
559void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
560{
561 struct rxrpc_call *call;
562 struct rb_node *p;
563
564 _enter("%p", rx);
565
566 read_lock_bh(&rx->call_lock);
567
David Howells17926a72007-04-26 15:48:28 -0700568 /* kill the not-yet-accepted incoming calls */
569 list_for_each_entry(call, &rx->secureq, accept_link) {
570 rxrpc_mark_call_released(call);
571 }
572
573 list_for_each_entry(call, &rx->acceptq, accept_link) {
574 rxrpc_mark_call_released(call);
575 }
576
David Howellsf36b5e42016-08-23 15:27:24 +0100577 /* mark all the calls as no longer wanting incoming packets */
578 for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
579 call = rb_entry(p, struct rxrpc_call, sock_node);
580 rxrpc_mark_call_released(call);
581 }
582
David Howells17926a72007-04-26 15:48:28 -0700583 read_unlock_bh(&rx->call_lock);
584 _leave("");
585}
586
587/*
588 * release a call
589 */
590void __rxrpc_put_call(struct rxrpc_call *call)
591{
592 ASSERT(call != NULL);
593
594 _enter("%p{u=%d}", call, atomic_read(&call->usage));
595
596 ASSERTCMP(atomic_read(&call->usage), >, 0);
597
598 if (atomic_dec_and_test(&call->usage)) {
599 _debug("call %d dead", call->debug_id);
David Howells372ee162016-08-03 14:11:40 +0100600 WARN_ON(atomic_read(&call->skb_count) != 0);
David Howells17926a72007-04-26 15:48:28 -0700601 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
David Howells651350d2007-04-26 15:50:17 -0700602 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700603 }
604 _leave("");
605}
606
607/*
David Howellsdee46362016-06-27 17:11:19 +0100608 * Final call destruction under RCU.
609 */
610static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
611{
612 struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
613
614 rxrpc_purge_queue(&call->rx_queue);
David Howellsdf5d8bf2016-08-24 14:31:43 +0100615 rxrpc_put_peer(call->peer);
David Howellsdee46362016-06-27 17:11:19 +0100616 kmem_cache_free(rxrpc_call_jar, call);
617}
618
619/*
David Howells17926a72007-04-26 15:48:28 -0700620 * clean up a call
621 */
622static void rxrpc_cleanup_call(struct rxrpc_call *call)
623{
624 _net("DESTROY CALL %d", call->debug_id);
625
626 ASSERT(call->socket);
627
628 memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
629
630 del_timer_sync(&call->lifetimer);
631 del_timer_sync(&call->deadspan);
632 del_timer_sync(&call->ack_timer);
633 del_timer_sync(&call->resend_timer);
634
635 ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
636 ASSERTCMP(call->events, ==, 0);
637 if (work_pending(&call->processor)) {
638 _debug("defer destroy");
David Howells651350d2007-04-26 15:50:17 -0700639 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700640 return;
641 }
642
David Howellse653cfe2016-04-04 14:00:38 +0100643 ASSERTCMP(call->conn, ==, NULL);
David Howells17926a72007-04-26 15:48:28 -0700644
645 if (call->acks_window) {
646 _debug("kill Tx window %d",
647 CIRC_CNT(call->acks_head, call->acks_tail,
648 call->acks_winsz));
649 smp_mb();
650 while (CIRC_CNT(call->acks_head, call->acks_tail,
651 call->acks_winsz) > 0) {
652 struct rxrpc_skb_priv *sp;
653 unsigned long _skb;
654
655 _skb = call->acks_window[call->acks_tail] & ~1;
David Howells0d12f8a2016-03-04 15:53:46 +0000656 sp = rxrpc_skb((struct sk_buff *)_skb);
657 _debug("+++ clear Tx %u", sp->hdr.seq);
658 rxrpc_free_skb((struct sk_buff *)_skb);
David Howells17926a72007-04-26 15:48:28 -0700659 call->acks_tail =
660 (call->acks_tail + 1) & (call->acks_winsz - 1);
661 }
662
663 kfree(call->acks_window);
664 }
665
666 rxrpc_free_skb(call->tx_pending);
667
668 rxrpc_purge_queue(&call->rx_queue);
669 ASSERT(skb_queue_empty(&call->rx_oos_queue));
670 sock_put(&call->socket->sk);
David Howellsdee46362016-06-27 17:11:19 +0100671 call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
David Howells17926a72007-04-26 15:48:28 -0700672}
673
674/*
675 * destroy a call
676 */
677static void rxrpc_destroy_call(struct work_struct *work)
678{
679 struct rxrpc_call *call =
680 container_of(work, struct rxrpc_call, destroyer);
681
David Howells01a90a42016-08-23 15:27:24 +0100682 _enter("%p{%d,%x,%p}",
683 call, atomic_read(&call->usage), call->cid, call->conn);
David Howells17926a72007-04-26 15:48:28 -0700684
685 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
686
687 write_lock_bh(&rxrpc_call_lock);
688 list_del_init(&call->link);
689 write_unlock_bh(&rxrpc_call_lock);
690
691 rxrpc_cleanup_call(call);
692 _leave("");
693}
694
695/*
696 * preemptively destroy all the call records from a transport endpoint rather
697 * than waiting for them to time out
698 */
699void __exit rxrpc_destroy_all_calls(void)
700{
701 struct rxrpc_call *call;
702
703 _enter("");
704 write_lock_bh(&rxrpc_call_lock);
705
706 while (!list_empty(&rxrpc_calls)) {
707 call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
708 _debug("Zapping call %p", call);
709
710 list_del_init(&call->link);
711
712 switch (atomic_read(&call->usage)) {
713 case 0:
714 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
715 break;
716 case 1:
717 if (del_timer_sync(&call->deadspan) != 0 &&
718 call->state != RXRPC_CALL_DEAD)
719 rxrpc_dead_call_expired((unsigned long) call);
720 if (call->state != RXRPC_CALL_DEAD)
721 break;
722 default:
Joe Perches9b6d5392016-06-02 12:08:52 -0700723 pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
David Howells17926a72007-04-26 15:48:28 -0700724 call, atomic_read(&call->usage),
725 atomic_read(&call->ackr_not_idle),
726 rxrpc_call_states[call->state],
727 call->flags, call->events);
728 if (!skb_queue_empty(&call->rx_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700729 pr_err("Rx queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700730 if (!skb_queue_empty(&call->rx_oos_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700731 pr_err("OOS queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700732 break;
733 }
734
735 write_unlock_bh(&rxrpc_call_lock);
736 cond_resched();
737 write_lock_bh(&rxrpc_call_lock);
738 }
739
740 write_unlock_bh(&rxrpc_call_lock);
741 _leave("");
742}
743
744/*
745 * handle call lifetime being exceeded
746 */
747static void rxrpc_call_life_expired(unsigned long _call)
748{
749 struct rxrpc_call *call = (struct rxrpc_call *) _call;
750
751 if (call->state >= RXRPC_CALL_COMPLETE)
752 return;
753
754 _enter("{%d}", call->debug_id);
755 read_lock_bh(&call->state_lock);
756 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells4c198ad2016-03-04 15:53:46 +0000757 set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700758 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700759 }
760 read_unlock_bh(&call->state_lock);
761}
762
763/*
764 * handle resend timer expiry
David Howells3b5bac22010-08-04 02:34:17 +0000765 * - may not take call->state_lock as this can deadlock against del_timer_sync()
David Howells17926a72007-04-26 15:48:28 -0700766 */
767static void rxrpc_resend_time_expired(unsigned long _call)
768{
769 struct rxrpc_call *call = (struct rxrpc_call *) _call;
770
771 _enter("{%d}", call->debug_id);
772
773 if (call->state >= RXRPC_CALL_COMPLETE)
774 return;
775
David Howells17926a72007-04-26 15:48:28 -0700776 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells4c198ad2016-03-04 15:53:46 +0000777 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700778 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700779}
780
781/*
782 * handle ACK timer expiry
783 */
784static void rxrpc_ack_time_expired(unsigned long _call)
785{
786 struct rxrpc_call *call = (struct rxrpc_call *) _call;
787
788 _enter("{%d}", call->debug_id);
789
790 if (call->state >= RXRPC_CALL_COMPLETE)
791 return;
792
793 read_lock_bh(&call->state_lock);
794 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000795 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700796 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700797 read_unlock_bh(&call->state_lock);
798}