blob: 4af01805bfc7126029c9074dfe2dc5beb6119ce4 [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;
318 candidate->cid = sp->hdr.cid;
319 candidate->call_id = sp->hdr.callNumber;
David Howells42886ff2016-06-16 13:31:07 +0100320 candidate->rx_data_post = 0;
321 candidate->state = RXRPC_CALL_SERVER_ACCEPTING;
David Howellsdabe5a72016-08-23 15:27:24 +0100322 candidate->flags |= (1 << RXRPC_CALL_IS_SERVICE);
David Howells17926a72007-04-26 15:48:28 -0700323 if (conn->security_ix > 0)
324 candidate->state = RXRPC_CALL_SERVER_SECURING;
325
David Howellsa1399f82016-06-27 14:39:44 +0100326 spin_lock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700327
328 /* set the channel for this call */
David Howellsa1399f82016-06-27 14:39:44 +0100329 call = rcu_dereference_protected(conn->channels[chan].call,
330 lockdep_is_held(&conn->channel_lock));
331
David Howells01a90a42016-08-23 15:27:24 +0100332 _debug("channel[%u] is %p", candidate->cid & RXRPC_CHANNELMASK, call);
David Howells42886ff2016-06-16 13:31:07 +0100333 if (call && call->call_id == sp->hdr.callNumber) {
David Howells17926a72007-04-26 15:48:28 -0700334 /* already set; must've been a duplicate packet */
335 _debug("extant call [%d]", call->state);
336 ASSERTCMP(call->conn, ==, conn);
337
338 read_lock(&call->state_lock);
339 switch (call->state) {
340 case RXRPC_CALL_LOCALLY_ABORTED:
David Howells4c198ad2016-03-04 15:53:46 +0000341 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700342 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700343 case RXRPC_CALL_REMOTELY_ABORTED:
344 read_unlock(&call->state_lock);
345 goto aborted_call;
346 default:
347 rxrpc_get_call(call);
348 read_unlock(&call->state_lock);
349 goto extant_call;
350 }
351 }
352
353 if (call) {
354 /* it seems the channel is still in use from the previous call
355 * - ditch the old binding if its call is now complete */
356 _debug("CALL: %u { %s }",
357 call->debug_id, rxrpc_call_states[call->state]);
358
359 if (call->state >= RXRPC_CALL_COMPLETE) {
David Howellsa1399f82016-06-27 14:39:44 +0100360 __rxrpc_disconnect_call(call);
David Howells17926a72007-04-26 15:48:28 -0700361 } else {
David Howellsa1399f82016-06-27 14:39:44 +0100362 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700363 kmem_cache_free(rxrpc_call_jar, candidate);
364 _leave(" = -EBUSY");
365 return ERR_PTR(-EBUSY);
366 }
367 }
368
369 /* check the call number isn't duplicate */
370 _debug("check dup");
David Howells42886ff2016-06-16 13:31:07 +0100371 call_id = sp->hdr.callNumber;
David Howells17926a72007-04-26 15:48:28 -0700372
David Howellsa1399f82016-06-27 14:39:44 +0100373 /* We just ignore calls prior to the current call ID. Terminated calls
374 * are handled via the connection.
375 */
376 if (call_id <= conn->channels[chan].call_counter)
377 goto old_call; /* TODO: Just drop packet */
David Howells17926a72007-04-26 15:48:28 -0700378
379 /* make the call available */
380 _debug("new call");
381 call = candidate;
382 candidate = NULL;
David Howellsa1399f82016-06-27 14:39:44 +0100383 conn->channels[chan].call_counter = call_id;
384 rcu_assign_pointer(conn->channels[chan].call, call);
David Howells17926a72007-04-26 15:48:28 -0700385 sock_hold(&rx->sk);
David Howells5627cc82016-04-04 14:00:38 +0100386 rxrpc_get_connection(conn);
David Howellsa1399f82016-06-27 14:39:44 +0100387 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700388
David Howells85f32272016-04-04 14:00:36 +0100389 spin_lock(&conn->params.peer->lock);
390 hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
391 spin_unlock(&conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700392
393 write_lock_bh(&rxrpc_call_lock);
394 list_add_tail(&call->link, &rxrpc_calls);
395 write_unlock_bh(&rxrpc_call_lock);
396
David Howells19ffa012016-04-04 14:00:36 +0100397 call->service_id = conn->params.service_id;
Tim Smith77276402014-03-03 23:04:45 +0000398
David Howells17926a72007-04-26 15:48:28 -0700399 _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
400
David Howells5873c082014-02-07 18:58:44 +0000401 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700402 add_timer(&call->lifetimer);
403 _leave(" = %p {%d} [new]", call, call->debug_id);
404 return call;
405
406extant_call:
David Howellsa1399f82016-06-27 14:39:44 +0100407 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700408 kmem_cache_free(rxrpc_call_jar, candidate);
409 _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
410 return call;
411
412aborted_call:
David Howellsa1399f82016-06-27 14:39:44 +0100413 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700414 kmem_cache_free(rxrpc_call_jar, candidate);
415 _leave(" = -ECONNABORTED");
416 return ERR_PTR(-ECONNABORTED);
417
418old_call:
David Howellsa1399f82016-06-27 14:39:44 +0100419 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700420 kmem_cache_free(rxrpc_call_jar, candidate);
421 _leave(" = -ECONNRESET [old]");
422 return ERR_PTR(-ECONNRESET);
423}
424
425/*
David Howells17926a72007-04-26 15:48:28 -0700426 * detach a call from a socket and set up for release
427 */
428void rxrpc_release_call(struct rxrpc_call *call)
429{
David Howells651350d2007-04-26 15:50:17 -0700430 struct rxrpc_connection *conn = call->conn;
David Howells17926a72007-04-26 15:48:28 -0700431 struct rxrpc_sock *rx = call->socket;
432
433 _enter("{%d,%d,%d,%d}",
434 call->debug_id, atomic_read(&call->usage),
435 atomic_read(&call->ackr_not_idle),
436 call->rx_first_oos);
437
438 spin_lock_bh(&call->lock);
439 if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
440 BUG();
441 spin_unlock_bh(&call->lock);
442
443 /* dissociate from the socket
444 * - the socket's ref on the call is passed to the death timer
445 */
David Howells651350d2007-04-26 15:50:17 -0700446 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
David Howells17926a72007-04-26 15:48:28 -0700447
David Howellse653cfe2016-04-04 14:00:38 +0100448 spin_lock(&conn->params.peer->lock);
449 hlist_del_init(&call->error_link);
450 spin_unlock(&conn->params.peer->lock);
451
David Howells17926a72007-04-26 15:48:28 -0700452 write_lock_bh(&rx->call_lock);
453 if (!list_empty(&call->accept_link)) {
454 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
455 call, call->events, call->flags);
456 ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
457 list_del_init(&call->accept_link);
458 sk_acceptq_removed(&rx->sk);
459 } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
460 rb_erase(&call->sock_node, &rx->calls);
461 memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
462 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
463 }
464 write_unlock_bh(&rx->call_lock);
465
David Howells17926a72007-04-26 15:48:28 -0700466 /* free up the channel for reuse */
David Howellsa1399f82016-06-27 14:39:44 +0100467 write_lock_bh(&call->state_lock);
David Howells651350d2007-04-26 15:50:17 -0700468
David Howells17926a72007-04-26 15:48:28 -0700469 if (call->state < RXRPC_CALL_COMPLETE &&
470 call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
471 _debug("+++ ABORTING STATE %d +++\n", call->state);
472 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100473 call->local_abort = RX_CALL_DEAD;
David Howells17926a72007-04-26 15:48:28 -0700474 }
David Howellsa1399f82016-06-27 14:39:44 +0100475 write_unlock_bh(&call->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700476
David Howellse653cfe2016-04-04 14:00:38 +0100477 rxrpc_disconnect_call(call);
478
David Howells651350d2007-04-26 15:50:17 -0700479 /* clean up the Rx queue */
David Howells17926a72007-04-26 15:48:28 -0700480 if (!skb_queue_empty(&call->rx_queue) ||
481 !skb_queue_empty(&call->rx_oos_queue)) {
482 struct rxrpc_skb_priv *sp;
483 struct sk_buff *skb;
484
485 _debug("purge Rx queues");
486
487 spin_lock_bh(&call->lock);
488 while ((skb = skb_dequeue(&call->rx_queue)) ||
489 (skb = skb_dequeue(&call->rx_oos_queue))) {
David Howells17926a72007-04-26 15:48:28 -0700490 spin_unlock_bh(&call->lock);
491
Arnd Bergmann55cae7a2016-08-08 12:13:45 +0200492 sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700493 _debug("- zap %s %%%u #%u",
494 rxrpc_pkts[sp->hdr.type],
David Howells0d12f8a2016-03-04 15:53:46 +0000495 sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700496 rxrpc_free_skb(skb);
497 spin_lock_bh(&call->lock);
498 }
499 spin_unlock_bh(&call->lock);
500
501 ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
502 }
503
504 del_timer_sync(&call->resend_timer);
505 del_timer_sync(&call->ack_timer);
506 del_timer_sync(&call->lifetimer);
David Howells5873c082014-02-07 18:58:44 +0000507 call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
David Howells17926a72007-04-26 15:48:28 -0700508 add_timer(&call->deadspan);
509
510 _leave("");
511}
512
513/*
514 * handle a dead call being ready for reaping
515 */
516static void rxrpc_dead_call_expired(unsigned long _call)
517{
518 struct rxrpc_call *call = (struct rxrpc_call *) _call;
519
520 _enter("{%d}", call->debug_id);
521
522 write_lock_bh(&call->state_lock);
523 call->state = RXRPC_CALL_DEAD;
524 write_unlock_bh(&call->state_lock);
525 rxrpc_put_call(call);
526}
527
528/*
529 * mark a call as to be released, aborting it if it's still in progress
530 * - called with softirqs disabled
531 */
532static void rxrpc_mark_call_released(struct rxrpc_call *call)
533{
534 bool sched;
535
536 write_lock(&call->state_lock);
537 if (call->state < RXRPC_CALL_DEAD) {
538 sched = false;
539 if (call->state < RXRPC_CALL_COMPLETE) {
540 _debug("abort call %p", call);
541 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100542 call->local_abort = RX_CALL_DEAD;
David Howells4c198ad2016-03-04 15:53:46 +0000543 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700544 sched = true;
545 }
David Howells4c198ad2016-03-04 15:53:46 +0000546 if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700547 sched = true;
548 if (sched)
David Howells651350d2007-04-26 15:50:17 -0700549 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700550 }
551 write_unlock(&call->state_lock);
552}
553
554/*
555 * release all the calls associated with a socket
556 */
557void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
558{
559 struct rxrpc_call *call;
560 struct rb_node *p;
561
562 _enter("%p", rx);
563
564 read_lock_bh(&rx->call_lock);
565
David Howells17926a72007-04-26 15:48:28 -0700566 /* kill the not-yet-accepted incoming calls */
567 list_for_each_entry(call, &rx->secureq, accept_link) {
568 rxrpc_mark_call_released(call);
569 }
570
571 list_for_each_entry(call, &rx->acceptq, accept_link) {
572 rxrpc_mark_call_released(call);
573 }
574
David Howellsf36b5e42016-08-23 15:27:24 +0100575 /* mark all the calls as no longer wanting incoming packets */
576 for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
577 call = rb_entry(p, struct rxrpc_call, sock_node);
578 rxrpc_mark_call_released(call);
579 }
580
David Howells17926a72007-04-26 15:48:28 -0700581 read_unlock_bh(&rx->call_lock);
582 _leave("");
583}
584
585/*
586 * release a call
587 */
588void __rxrpc_put_call(struct rxrpc_call *call)
589{
590 ASSERT(call != NULL);
591
592 _enter("%p{u=%d}", call, atomic_read(&call->usage));
593
594 ASSERTCMP(atomic_read(&call->usage), >, 0);
595
596 if (atomic_dec_and_test(&call->usage)) {
597 _debug("call %d dead", call->debug_id);
David Howells372ee162016-08-03 14:11:40 +0100598 WARN_ON(atomic_read(&call->skb_count) != 0);
David Howells17926a72007-04-26 15:48:28 -0700599 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
David Howells651350d2007-04-26 15:50:17 -0700600 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700601 }
602 _leave("");
603}
604
605/*
David Howellsdee46362016-06-27 17:11:19 +0100606 * Final call destruction under RCU.
607 */
608static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
609{
610 struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
611
612 rxrpc_purge_queue(&call->rx_queue);
613 kmem_cache_free(rxrpc_call_jar, call);
614}
615
616/*
David Howells17926a72007-04-26 15:48:28 -0700617 * clean up a call
618 */
619static void rxrpc_cleanup_call(struct rxrpc_call *call)
620{
621 _net("DESTROY CALL %d", call->debug_id);
622
623 ASSERT(call->socket);
624
625 memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
626
627 del_timer_sync(&call->lifetimer);
628 del_timer_sync(&call->deadspan);
629 del_timer_sync(&call->ack_timer);
630 del_timer_sync(&call->resend_timer);
631
632 ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
633 ASSERTCMP(call->events, ==, 0);
634 if (work_pending(&call->processor)) {
635 _debug("defer destroy");
David Howells651350d2007-04-26 15:50:17 -0700636 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700637 return;
638 }
639
David Howellse653cfe2016-04-04 14:00:38 +0100640 ASSERTCMP(call->conn, ==, NULL);
David Howells17926a72007-04-26 15:48:28 -0700641
642 if (call->acks_window) {
643 _debug("kill Tx window %d",
644 CIRC_CNT(call->acks_head, call->acks_tail,
645 call->acks_winsz));
646 smp_mb();
647 while (CIRC_CNT(call->acks_head, call->acks_tail,
648 call->acks_winsz) > 0) {
649 struct rxrpc_skb_priv *sp;
650 unsigned long _skb;
651
652 _skb = call->acks_window[call->acks_tail] & ~1;
David Howells0d12f8a2016-03-04 15:53:46 +0000653 sp = rxrpc_skb((struct sk_buff *)_skb);
654 _debug("+++ clear Tx %u", sp->hdr.seq);
655 rxrpc_free_skb((struct sk_buff *)_skb);
David Howells17926a72007-04-26 15:48:28 -0700656 call->acks_tail =
657 (call->acks_tail + 1) & (call->acks_winsz - 1);
658 }
659
660 kfree(call->acks_window);
661 }
662
663 rxrpc_free_skb(call->tx_pending);
664
665 rxrpc_purge_queue(&call->rx_queue);
666 ASSERT(skb_queue_empty(&call->rx_oos_queue));
667 sock_put(&call->socket->sk);
David Howellsdee46362016-06-27 17:11:19 +0100668 call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
David Howells17926a72007-04-26 15:48:28 -0700669}
670
671/*
672 * destroy a call
673 */
674static void rxrpc_destroy_call(struct work_struct *work)
675{
676 struct rxrpc_call *call =
677 container_of(work, struct rxrpc_call, destroyer);
678
David Howells01a90a42016-08-23 15:27:24 +0100679 _enter("%p{%d,%x,%p}",
680 call, atomic_read(&call->usage), call->cid, call->conn);
David Howells17926a72007-04-26 15:48:28 -0700681
682 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
683
684 write_lock_bh(&rxrpc_call_lock);
685 list_del_init(&call->link);
686 write_unlock_bh(&rxrpc_call_lock);
687
688 rxrpc_cleanup_call(call);
689 _leave("");
690}
691
692/*
693 * preemptively destroy all the call records from a transport endpoint rather
694 * than waiting for them to time out
695 */
696void __exit rxrpc_destroy_all_calls(void)
697{
698 struct rxrpc_call *call;
699
700 _enter("");
701 write_lock_bh(&rxrpc_call_lock);
702
703 while (!list_empty(&rxrpc_calls)) {
704 call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
705 _debug("Zapping call %p", call);
706
707 list_del_init(&call->link);
708
709 switch (atomic_read(&call->usage)) {
710 case 0:
711 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
712 break;
713 case 1:
714 if (del_timer_sync(&call->deadspan) != 0 &&
715 call->state != RXRPC_CALL_DEAD)
716 rxrpc_dead_call_expired((unsigned long) call);
717 if (call->state != RXRPC_CALL_DEAD)
718 break;
719 default:
Joe Perches9b6d5392016-06-02 12:08:52 -0700720 pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
David Howells17926a72007-04-26 15:48:28 -0700721 call, atomic_read(&call->usage),
722 atomic_read(&call->ackr_not_idle),
723 rxrpc_call_states[call->state],
724 call->flags, call->events);
725 if (!skb_queue_empty(&call->rx_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700726 pr_err("Rx queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700727 if (!skb_queue_empty(&call->rx_oos_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700728 pr_err("OOS queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700729 break;
730 }
731
732 write_unlock_bh(&rxrpc_call_lock);
733 cond_resched();
734 write_lock_bh(&rxrpc_call_lock);
735 }
736
737 write_unlock_bh(&rxrpc_call_lock);
738 _leave("");
739}
740
741/*
742 * handle call lifetime being exceeded
743 */
744static void rxrpc_call_life_expired(unsigned long _call)
745{
746 struct rxrpc_call *call = (struct rxrpc_call *) _call;
747
748 if (call->state >= RXRPC_CALL_COMPLETE)
749 return;
750
751 _enter("{%d}", call->debug_id);
752 read_lock_bh(&call->state_lock);
753 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells4c198ad2016-03-04 15:53:46 +0000754 set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700755 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700756 }
757 read_unlock_bh(&call->state_lock);
758}
759
760/*
761 * handle resend timer expiry
David Howells3b5bac22010-08-04 02:34:17 +0000762 * - may not take call->state_lock as this can deadlock against del_timer_sync()
David Howells17926a72007-04-26 15:48:28 -0700763 */
764static void rxrpc_resend_time_expired(unsigned long _call)
765{
766 struct rxrpc_call *call = (struct rxrpc_call *) _call;
767
768 _enter("{%d}", call->debug_id);
769
770 if (call->state >= RXRPC_CALL_COMPLETE)
771 return;
772
David Howells17926a72007-04-26 15:48:28 -0700773 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells4c198ad2016-03-04 15:53:46 +0000774 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700775 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700776}
777
778/*
779 * handle ACK timer expiry
780 */
781static void rxrpc_ack_time_expired(unsigned long _call)
782{
783 struct rxrpc_call *call = (struct rxrpc_call *) _call;
784
785 _enter("{%d}", call->debug_id);
786
787 if (call->state >= RXRPC_CALL_COMPLETE)
788 return;
789
790 read_lock_bh(&call->state_lock);
791 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000792 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700793 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700794 read_unlock_bh(&call->state_lock);
795}