blob: 91287c9d01bb460c56eda06d3d4e7ceea074f14e [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;
170
David Howells999b69f2016-06-17 15:42:35 +0100171 call->local = rx->local;
David Howells999b69f2016-06-17 15:42:35 +0100172 call->service_id = srx->srx_service;
173 call->in_clientflag = 0;
174
175 _leave(" = %p", call);
176 return call;
177}
178
179/*
180 * Begin client call.
181 */
182static int rxrpc_begin_client_call(struct rxrpc_call *call,
183 struct rxrpc_conn_parameters *cp,
David Howells999b69f2016-06-17 15:42:35 +0100184 struct sockaddr_rxrpc *srx,
185 gfp_t gfp)
186{
187 int ret;
188
189 /* Set up or get a connection record and set the protocol parameters,
190 * including channel number and call ID.
191 */
David Howellsaa390bb2016-06-17 10:06:56 +0100192 ret = rxrpc_connect_call(call, cp, srx, gfp);
David Howells999b69f2016-06-17 15:42:35 +0100193 if (ret < 0)
194 return ret;
195
196 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
197
David Howells85f32272016-04-04 14:00:36 +0100198 spin_lock(&call->conn->params.peer->lock);
199 hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
200 spin_unlock(&call->conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700201
David Howells5873c082014-02-07 18:58:44 +0000202 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700203 add_timer(&call->lifetimer);
David Howells999b69f2016-06-17 15:42:35 +0100204 return 0;
David Howells17926a72007-04-26 15:48:28 -0700205}
206
207/*
208 * set up a call for the given data
209 * - called in process context with IRQs enabled
210 */
David Howells2341e072016-06-09 23:02:51 +0100211struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100212 struct rxrpc_conn_parameters *cp,
David Howells999b69f2016-06-17 15:42:35 +0100213 struct sockaddr_rxrpc *srx,
David Howells17926a72007-04-26 15:48:28 -0700214 unsigned long user_call_ID,
David Howells17926a72007-04-26 15:48:28 -0700215 gfp_t gfp)
216{
David Howells2341e072016-06-09 23:02:51 +0100217 struct rxrpc_call *call, *xcall;
218 struct rb_node *parent, **pp;
David Howells999b69f2016-06-17 15:42:35 +0100219 int ret;
David Howells17926a72007-04-26 15:48:28 -0700220
David Howells999b69f2016-06-17 15:42:35 +0100221 _enter("%p,%lx", rx, user_call_ID);
David Howells17926a72007-04-26 15:48:28 -0700222
David Howellsaa390bb2016-06-17 10:06:56 +0100223 call = rxrpc_alloc_client_call(rx, srx, gfp);
David Howells2341e072016-06-09 23:02:51 +0100224 if (IS_ERR(call)) {
225 _leave(" = %ld", PTR_ERR(call));
226 return call;
David Howells17926a72007-04-26 15:48:28 -0700227 }
228
David Howells999b69f2016-06-17 15:42:35 +0100229 /* Publish the call, even though it is incompletely set up as yet */
David Howells2341e072016-06-09 23:02:51 +0100230 call->user_call_ID = user_call_ID;
231 __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
David Howells17926a72007-04-26 15:48:28 -0700232
233 write_lock(&rx->call_lock);
234
235 pp = &rx->calls.rb_node;
236 parent = NULL;
237 while (*pp) {
238 parent = *pp;
David Howells2341e072016-06-09 23:02:51 +0100239 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
David Howells17926a72007-04-26 15:48:28 -0700240
David Howells2341e072016-06-09 23:02:51 +0100241 if (user_call_ID < xcall->user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700242 pp = &(*pp)->rb_left;
David Howells2341e072016-06-09 23:02:51 +0100243 else if (user_call_ID > xcall->user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700244 pp = &(*pp)->rb_right;
245 else
David Howells2341e072016-06-09 23:02:51 +0100246 goto found_user_ID_now_present;
David Howells17926a72007-04-26 15:48:28 -0700247 }
248
David Howells17926a72007-04-26 15:48:28 -0700249 rxrpc_get_call(call);
250
251 rb_link_node(&call->sock_node, parent, pp);
252 rb_insert_color(&call->sock_node, &rx->calls);
253 write_unlock(&rx->call_lock);
254
255 write_lock_bh(&rxrpc_call_lock);
256 list_add_tail(&call->link, &rxrpc_calls);
257 write_unlock_bh(&rxrpc_call_lock);
258
David Howellsaa390bb2016-06-17 10:06:56 +0100259 ret = rxrpc_begin_client_call(call, cp, srx, gfp);
David Howells999b69f2016-06-17 15:42:35 +0100260 if (ret < 0)
261 goto error;
262
David Howells17926a72007-04-26 15:48:28 -0700263 _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
264
265 _leave(" = %p [new]", call);
266 return call;
267
David Howells999b69f2016-06-17 15:42:35 +0100268error:
269 write_lock(&rx->call_lock);
270 rb_erase(&call->sock_node, &rx->calls);
271 write_unlock(&rx->call_lock);
272 rxrpc_put_call(call);
273
274 write_lock_bh(&rxrpc_call_lock);
David Howellsd1e858c2016-04-04 14:00:39 +0100275 list_del_init(&call->link);
David Howells999b69f2016-06-17 15:42:35 +0100276 write_unlock_bh(&rxrpc_call_lock);
277
David Howellsd1e858c2016-04-04 14:00:39 +0100278 call->state = RXRPC_CALL_DEAD;
David Howells999b69f2016-06-17 15:42:35 +0100279 rxrpc_put_call(call);
280 _leave(" = %d", ret);
281 return ERR_PTR(ret);
282
David Howells2341e072016-06-09 23:02:51 +0100283 /* We unexpectedly found the user ID in the list after taking
284 * the call_lock. This shouldn't happen unless the user races
285 * with itself and tries to add the same user ID twice at the
286 * same time in different threads.
287 */
288found_user_ID_now_present:
David Howells17926a72007-04-26 15:48:28 -0700289 write_unlock(&rx->call_lock);
David Howellsd1e858c2016-04-04 14:00:39 +0100290 call->state = RXRPC_CALL_DEAD;
David Howells2341e072016-06-09 23:02:51 +0100291 rxrpc_put_call(call);
292 _leave(" = -EEXIST [%p]", call);
293 return ERR_PTR(-EEXIST);
David Howells17926a72007-04-26 15:48:28 -0700294}
295
296/*
297 * set up an incoming call
298 * - called in process context with IRQs enabled
299 */
300struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
301 struct rxrpc_connection *conn,
David Howells42886ff2016-06-16 13:31:07 +0100302 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700303{
David Howells42886ff2016-06-16 13:31:07 +0100304 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700305 struct rxrpc_call *call, *candidate;
David Howellsa1399f82016-06-27 14:39:44 +0100306 u32 call_id, chan;
David Howells17926a72007-04-26 15:48:28 -0700307
David Howells843099c2016-04-07 17:23:37 +0100308 _enter(",%d", conn->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700309
310 ASSERT(rx != NULL);
311
David Howells843099c2016-04-07 17:23:37 +0100312 candidate = rxrpc_alloc_call(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700313 if (!candidate)
314 return ERR_PTR(-EBUSY);
315
David Howellsa1399f82016-06-27 14:39:44 +0100316 chan = sp->hdr.cid & RXRPC_CHANNELMASK;
David Howells42886ff2016-06-16 13:31:07 +0100317 candidate->socket = rx;
318 candidate->conn = conn;
319 candidate->cid = sp->hdr.cid;
320 candidate->call_id = sp->hdr.callNumber;
David Howellsa1399f82016-06-27 14:39:44 +0100321 candidate->channel = chan;
David Howells42886ff2016-06-16 13:31:07 +0100322 candidate->rx_data_post = 0;
323 candidate->state = RXRPC_CALL_SERVER_ACCEPTING;
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 Howells17926a72007-04-26 15:48:28 -0700333 _debug("channel[%u] is %p", candidate->channel, 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 Howellsa1399f82016-06-27 14:39:44 +0100388 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700389
David Howells85f32272016-04-04 14:00:36 +0100390 spin_lock(&conn->params.peer->lock);
391 hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
392 spin_unlock(&conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700393
394 write_lock_bh(&rxrpc_call_lock);
395 list_add_tail(&call->link, &rxrpc_calls);
396 write_unlock_bh(&rxrpc_call_lock);
397
David Howells85f32272016-04-04 14:00:36 +0100398 call->local = conn->params.local;
David Howells19ffa012016-04-04 14:00:36 +0100399 call->epoch = conn->proto.epoch;
400 call->service_id = conn->params.service_id;
David Howellse8d70ce2016-06-30 12:16:21 +0100401 call->in_clientflag = RXRPC_CLIENT_INITIATED;
Tim Smith77276402014-03-03 23:04:45 +0000402
David Howells17926a72007-04-26 15:48:28 -0700403 _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
404
David Howells5873c082014-02-07 18:58:44 +0000405 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700406 add_timer(&call->lifetimer);
407 _leave(" = %p {%d} [new]", call, call->debug_id);
408 return call;
409
410extant_call:
David Howellsa1399f82016-06-27 14:39:44 +0100411 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700412 kmem_cache_free(rxrpc_call_jar, candidate);
413 _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
414 return call;
415
416aborted_call:
David Howellsa1399f82016-06-27 14:39:44 +0100417 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700418 kmem_cache_free(rxrpc_call_jar, candidate);
419 _leave(" = -ECONNABORTED");
420 return ERR_PTR(-ECONNABORTED);
421
422old_call:
David Howellsa1399f82016-06-27 14:39:44 +0100423 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700424 kmem_cache_free(rxrpc_call_jar, candidate);
425 _leave(" = -ECONNRESET [old]");
426 return ERR_PTR(-ECONNRESET);
427}
428
429/*
David Howells17926a72007-04-26 15:48:28 -0700430 * detach a call from a socket and set up for release
431 */
432void rxrpc_release_call(struct rxrpc_call *call)
433{
David Howells651350d2007-04-26 15:50:17 -0700434 struct rxrpc_connection *conn = call->conn;
David Howells17926a72007-04-26 15:48:28 -0700435 struct rxrpc_sock *rx = call->socket;
436
437 _enter("{%d,%d,%d,%d}",
438 call->debug_id, atomic_read(&call->usage),
439 atomic_read(&call->ackr_not_idle),
440 call->rx_first_oos);
441
442 spin_lock_bh(&call->lock);
443 if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
444 BUG();
445 spin_unlock_bh(&call->lock);
446
447 /* dissociate from the socket
448 * - the socket's ref on the call is passed to the death timer
449 */
David Howells651350d2007-04-26 15:50:17 -0700450 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
David Howells17926a72007-04-26 15:48:28 -0700451
David Howellse653cfe2016-04-04 14:00:38 +0100452 spin_lock(&conn->params.peer->lock);
453 hlist_del_init(&call->error_link);
454 spin_unlock(&conn->params.peer->lock);
455
David Howells17926a72007-04-26 15:48:28 -0700456 write_lock_bh(&rx->call_lock);
457 if (!list_empty(&call->accept_link)) {
458 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
459 call, call->events, call->flags);
460 ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
461 list_del_init(&call->accept_link);
462 sk_acceptq_removed(&rx->sk);
463 } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
464 rb_erase(&call->sock_node, &rx->calls);
465 memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
466 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
467 }
468 write_unlock_bh(&rx->call_lock);
469
David Howells17926a72007-04-26 15:48:28 -0700470 /* free up the channel for reuse */
David Howellsa1399f82016-06-27 14:39:44 +0100471 write_lock_bh(&call->state_lock);
David Howells651350d2007-04-26 15:50:17 -0700472
David Howells17926a72007-04-26 15:48:28 -0700473 if (call->state < RXRPC_CALL_COMPLETE &&
474 call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
475 _debug("+++ ABORTING STATE %d +++\n", call->state);
476 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100477 call->local_abort = RX_CALL_DEAD;
David Howells17926a72007-04-26 15:48:28 -0700478 }
David Howellsa1399f82016-06-27 14:39:44 +0100479 write_unlock_bh(&call->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700480
David Howellse653cfe2016-04-04 14:00:38 +0100481 rxrpc_disconnect_call(call);
482
David Howells651350d2007-04-26 15:50:17 -0700483 /* clean up the Rx queue */
David Howells17926a72007-04-26 15:48:28 -0700484 if (!skb_queue_empty(&call->rx_queue) ||
485 !skb_queue_empty(&call->rx_oos_queue)) {
486 struct rxrpc_skb_priv *sp;
487 struct sk_buff *skb;
488
489 _debug("purge Rx queues");
490
491 spin_lock_bh(&call->lock);
492 while ((skb = skb_dequeue(&call->rx_queue)) ||
493 (skb = skb_dequeue(&call->rx_oos_queue))) {
494 sp = rxrpc_skb(skb);
495 if (sp->call) {
496 ASSERTCMP(sp->call, ==, call);
497 rxrpc_put_call(call);
498 sp->call = NULL;
499 }
500 skb->destructor = NULL;
501 spin_unlock_bh(&call->lock);
502
503 _debug("- zap %s %%%u #%u",
504 rxrpc_pkts[sp->hdr.type],
David Howells0d12f8a2016-03-04 15:53:46 +0000505 sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700506 rxrpc_free_skb(skb);
507 spin_lock_bh(&call->lock);
508 }
509 spin_unlock_bh(&call->lock);
510
511 ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
512 }
513
514 del_timer_sync(&call->resend_timer);
515 del_timer_sync(&call->ack_timer);
516 del_timer_sync(&call->lifetimer);
David Howells5873c082014-02-07 18:58:44 +0000517 call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
David Howells17926a72007-04-26 15:48:28 -0700518 add_timer(&call->deadspan);
519
520 _leave("");
521}
522
523/*
524 * handle a dead call being ready for reaping
525 */
526static void rxrpc_dead_call_expired(unsigned long _call)
527{
528 struct rxrpc_call *call = (struct rxrpc_call *) _call;
529
530 _enter("{%d}", call->debug_id);
531
532 write_lock_bh(&call->state_lock);
533 call->state = RXRPC_CALL_DEAD;
534 write_unlock_bh(&call->state_lock);
535 rxrpc_put_call(call);
536}
537
538/*
539 * mark a call as to be released, aborting it if it's still in progress
540 * - called with softirqs disabled
541 */
542static void rxrpc_mark_call_released(struct rxrpc_call *call)
543{
544 bool sched;
545
546 write_lock(&call->state_lock);
547 if (call->state < RXRPC_CALL_DEAD) {
548 sched = false;
549 if (call->state < RXRPC_CALL_COMPLETE) {
550 _debug("abort call %p", call);
551 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100552 call->local_abort = RX_CALL_DEAD;
David Howells4c198ad2016-03-04 15:53:46 +0000553 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700554 sched = true;
555 }
David Howells4c198ad2016-03-04 15:53:46 +0000556 if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700557 sched = true;
558 if (sched)
David Howells651350d2007-04-26 15:50:17 -0700559 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700560 }
561 write_unlock(&call->state_lock);
562}
563
564/*
565 * release all the calls associated with a socket
566 */
567void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
568{
569 struct rxrpc_call *call;
570 struct rb_node *p;
571
572 _enter("%p", rx);
573
574 read_lock_bh(&rx->call_lock);
575
576 /* mark all the calls as no longer wanting incoming packets */
577 for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
578 call = rb_entry(p, struct rxrpc_call, sock_node);
579 rxrpc_mark_call_released(call);
580 }
581
582 /* kill the not-yet-accepted incoming calls */
583 list_for_each_entry(call, &rx->secureq, accept_link) {
584 rxrpc_mark_call_released(call);
585 }
586
587 list_for_each_entry(call, &rx->acceptq, accept_link) {
588 rxrpc_mark_call_released(call);
589 }
590
591 read_unlock_bh(&rx->call_lock);
592 _leave("");
593}
594
595/*
596 * release a call
597 */
598void __rxrpc_put_call(struct rxrpc_call *call)
599{
600 ASSERT(call != NULL);
601
602 _enter("%p{u=%d}", call, atomic_read(&call->usage));
603
604 ASSERTCMP(atomic_read(&call->usage), >, 0);
605
606 if (atomic_dec_and_test(&call->usage)) {
607 _debug("call %d dead", call->debug_id);
608 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
David Howells651350d2007-04-26 15:50:17 -0700609 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700610 }
611 _leave("");
612}
613
614/*
David Howellsdee46362016-06-27 17:11:19 +0100615 * Final call destruction under RCU.
616 */
617static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
618{
619 struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
620
621 rxrpc_purge_queue(&call->rx_queue);
622 kmem_cache_free(rxrpc_call_jar, call);
623}
624
625/*
David Howells17926a72007-04-26 15:48:28 -0700626 * clean up a call
627 */
628static void rxrpc_cleanup_call(struct rxrpc_call *call)
629{
630 _net("DESTROY CALL %d", call->debug_id);
631
632 ASSERT(call->socket);
633
634 memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
635
636 del_timer_sync(&call->lifetimer);
637 del_timer_sync(&call->deadspan);
638 del_timer_sync(&call->ack_timer);
639 del_timer_sync(&call->resend_timer);
640
641 ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
642 ASSERTCMP(call->events, ==, 0);
643 if (work_pending(&call->processor)) {
644 _debug("defer destroy");
David Howells651350d2007-04-26 15:50:17 -0700645 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700646 return;
647 }
648
David Howellse653cfe2016-04-04 14:00:38 +0100649 ASSERTCMP(call->conn, ==, NULL);
David Howells17926a72007-04-26 15:48:28 -0700650
651 if (call->acks_window) {
652 _debug("kill Tx window %d",
653 CIRC_CNT(call->acks_head, call->acks_tail,
654 call->acks_winsz));
655 smp_mb();
656 while (CIRC_CNT(call->acks_head, call->acks_tail,
657 call->acks_winsz) > 0) {
658 struct rxrpc_skb_priv *sp;
659 unsigned long _skb;
660
661 _skb = call->acks_window[call->acks_tail] & ~1;
David Howells0d12f8a2016-03-04 15:53:46 +0000662 sp = rxrpc_skb((struct sk_buff *)_skb);
663 _debug("+++ clear Tx %u", sp->hdr.seq);
664 rxrpc_free_skb((struct sk_buff *)_skb);
David Howells17926a72007-04-26 15:48:28 -0700665 call->acks_tail =
666 (call->acks_tail + 1) & (call->acks_winsz - 1);
667 }
668
669 kfree(call->acks_window);
670 }
671
672 rxrpc_free_skb(call->tx_pending);
673
674 rxrpc_purge_queue(&call->rx_queue);
675 ASSERT(skb_queue_empty(&call->rx_oos_queue));
676 sock_put(&call->socket->sk);
David Howellsdee46362016-06-27 17:11:19 +0100677 call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
David Howells17926a72007-04-26 15:48:28 -0700678}
679
680/*
681 * destroy a call
682 */
683static void rxrpc_destroy_call(struct work_struct *work)
684{
685 struct rxrpc_call *call =
686 container_of(work, struct rxrpc_call, destroyer);
687
688 _enter("%p{%d,%d,%p}",
689 call, atomic_read(&call->usage), call->channel, call->conn);
690
691 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
692
693 write_lock_bh(&rxrpc_call_lock);
694 list_del_init(&call->link);
695 write_unlock_bh(&rxrpc_call_lock);
696
697 rxrpc_cleanup_call(call);
698 _leave("");
699}
700
701/*
702 * preemptively destroy all the call records from a transport endpoint rather
703 * than waiting for them to time out
704 */
705void __exit rxrpc_destroy_all_calls(void)
706{
707 struct rxrpc_call *call;
708
709 _enter("");
710 write_lock_bh(&rxrpc_call_lock);
711
712 while (!list_empty(&rxrpc_calls)) {
713 call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
714 _debug("Zapping call %p", call);
715
716 list_del_init(&call->link);
717
718 switch (atomic_read(&call->usage)) {
719 case 0:
720 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
721 break;
722 case 1:
723 if (del_timer_sync(&call->deadspan) != 0 &&
724 call->state != RXRPC_CALL_DEAD)
725 rxrpc_dead_call_expired((unsigned long) call);
726 if (call->state != RXRPC_CALL_DEAD)
727 break;
728 default:
Joe Perches9b6d5392016-06-02 12:08:52 -0700729 pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
David Howells17926a72007-04-26 15:48:28 -0700730 call, atomic_read(&call->usage),
731 atomic_read(&call->ackr_not_idle),
732 rxrpc_call_states[call->state],
733 call->flags, call->events);
734 if (!skb_queue_empty(&call->rx_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700735 pr_err("Rx queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700736 if (!skb_queue_empty(&call->rx_oos_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700737 pr_err("OOS queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700738 break;
739 }
740
741 write_unlock_bh(&rxrpc_call_lock);
742 cond_resched();
743 write_lock_bh(&rxrpc_call_lock);
744 }
745
746 write_unlock_bh(&rxrpc_call_lock);
747 _leave("");
748}
749
750/*
751 * handle call lifetime being exceeded
752 */
753static void rxrpc_call_life_expired(unsigned long _call)
754{
755 struct rxrpc_call *call = (struct rxrpc_call *) _call;
756
757 if (call->state >= RXRPC_CALL_COMPLETE)
758 return;
759
760 _enter("{%d}", call->debug_id);
761 read_lock_bh(&call->state_lock);
762 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells4c198ad2016-03-04 15:53:46 +0000763 set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700764 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700765 }
766 read_unlock_bh(&call->state_lock);
767}
768
769/*
770 * handle resend timer expiry
David Howells3b5bac22010-08-04 02:34:17 +0000771 * - may not take call->state_lock as this can deadlock against del_timer_sync()
David Howells17926a72007-04-26 15:48:28 -0700772 */
773static void rxrpc_resend_time_expired(unsigned long _call)
774{
775 struct rxrpc_call *call = (struct rxrpc_call *) _call;
776
777 _enter("{%d}", call->debug_id);
778
779 if (call->state >= RXRPC_CALL_COMPLETE)
780 return;
781
David Howells17926a72007-04-26 15:48:28 -0700782 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells4c198ad2016-03-04 15:53:46 +0000783 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700784 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700785}
786
787/*
788 * handle ACK timer expiry
789 */
790static void rxrpc_ack_time_expired(unsigned long _call)
791{
792 struct rxrpc_call *call = (struct rxrpc_call *) _call;
793
794 _enter("{%d}", call->debug_id);
795
796 if (call->state >= RXRPC_CALL_COMPLETE)
797 return;
798
799 read_lock_bh(&call->state_lock);
800 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000801 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700802 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700803 read_unlock_bh(&call->state_lock);
804}