rxrpc: Fix a potential NULL-pointer deref in rxrpc_abort_calls
[linux-block.git] / net / rxrpc / call_object.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
5a0e3ad6 14#include <linux/slab.h>
17926a79
DH
15#include <linux/module.h>
16#include <linux/circ_buf.h>
7727640c 17#include <linux/spinlock_types.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
5873c083
DH
22/*
23 * Maximum lifetime of a call (in jiffies).
24 */
dad8aff7 25unsigned int rxrpc_max_call_lifetime = 60 * HZ;
5873c083
DH
26
27/*
28 * Time till dead call expires after last use (in jiffies).
29 */
dad8aff7 30unsigned int rxrpc_dead_call_expiry = 2 * HZ;
5873c083 31
5b8848d1 32const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
999b69f8
DH
33 [RXRPC_CALL_UNINITIALISED] = "Uninit",
34 [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn",
1f8481d1
DH
35 [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
17926a79
DH
53struct kmem_cache *rxrpc_call_jar;
54LIST_HEAD(rxrpc_calls);
55DEFINE_RWLOCK(rxrpc_call_lock);
17926a79
DH
56
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
2341e077
DH
63/*
64 * 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
17926a79
DH
100/*
101 * 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);
999b69f8 129 INIT_LIST_HEAD(&call->link);
45025bce 130 INIT_LIST_HEAD(&call->chan_wait_link);
17926a79
DH
131 INIT_LIST_HEAD(&call->accept_link);
132 skb_queue_head_init(&call->rx_queue);
133 skb_queue_head_init(&call->rx_oos_queue);
45025bce 134 init_waitqueue_head(&call->waitq);
17926a79
DH
135 spin_lock_init(&call->lock);
136 rwlock_init(&call->state_lock);
137 atomic_set(&call->usage, 1);
138 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
17926a79
DH
139
140 memset(&call->sock_node, 0xed, sizeof(call->sock_node));
141
142 call->rx_data_expect = 1;
143 call->rx_data_eaten = 0;
144 call->rx_first_oos = 0;
817913d8 145 call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
17926a79
DH
146 call->creation_jif = jiffies;
147 return call;
148}
149
150/*
999b69f8 151 * Allocate a new client call.
17926a79 152 */
aa390bbe
DH
153static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
154 struct sockaddr_rxrpc *srx,
155 gfp_t gfp)
17926a79
DH
156{
157 struct rxrpc_call *call;
17926a79
DH
158
159 _enter("");
160
999b69f8 161 ASSERT(rx->local != NULL);
17926a79
DH
162
163 call = rxrpc_alloc_call(gfp);
164 if (!call)
165 return ERR_PTR(-ENOMEM);
999b69f8 166 call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
17926a79
DH
167
168 sock_hold(&rx->sk);
169 call->socket = rx;
170 call->rx_data_post = 1;
999b69f8 171 call->service_id = srx->srx_service;
999b69f8
DH
172
173 _leave(" = %p", call);
174 return call;
175}
176
177/*
178 * Begin client call.
179 */
180static int rxrpc_begin_client_call(struct rxrpc_call *call,
181 struct rxrpc_conn_parameters *cp,
999b69f8
DH
182 struct sockaddr_rxrpc *srx,
183 gfp_t gfp)
184{
185 int ret;
186
187 /* Set up or get a connection record and set the protocol parameters,
188 * including channel number and call ID.
189 */
aa390bbe 190 ret = rxrpc_connect_call(call, cp, srx, gfp);
999b69f8
DH
191 if (ret < 0)
192 return ret;
193
194 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
195
85f32278
DH
196 spin_lock(&call->conn->params.peer->lock);
197 hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
198 spin_unlock(&call->conn->params.peer->lock);
17926a79 199
5873c083 200 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
17926a79 201 add_timer(&call->lifetimer);
999b69f8 202 return 0;
17926a79
DH
203}
204
205/*
206 * set up a call for the given data
207 * - called in process context with IRQs enabled
208 */
2341e077 209struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
19ffa01c 210 struct rxrpc_conn_parameters *cp,
999b69f8 211 struct sockaddr_rxrpc *srx,
17926a79 212 unsigned long user_call_ID,
17926a79
DH
213 gfp_t gfp)
214{
2341e077
DH
215 struct rxrpc_call *call, *xcall;
216 struct rb_node *parent, **pp;
999b69f8 217 int ret;
17926a79 218
999b69f8 219 _enter("%p,%lx", rx, user_call_ID);
17926a79 220
aa390bbe 221 call = rxrpc_alloc_client_call(rx, srx, gfp);
2341e077
DH
222 if (IS_ERR(call)) {
223 _leave(" = %ld", PTR_ERR(call));
224 return call;
17926a79
DH
225 }
226
999b69f8 227 /* Publish the call, even though it is incompletely set up as yet */
2341e077
DH
228 call->user_call_ID = user_call_ID;
229 __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
17926a79
DH
230
231 write_lock(&rx->call_lock);
232
233 pp = &rx->calls.rb_node;
234 parent = NULL;
235 while (*pp) {
236 parent = *pp;
2341e077 237 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
17926a79 238
2341e077 239 if (user_call_ID < xcall->user_call_ID)
17926a79 240 pp = &(*pp)->rb_left;
2341e077 241 else if (user_call_ID > xcall->user_call_ID)
17926a79
DH
242 pp = &(*pp)->rb_right;
243 else
2341e077 244 goto found_user_ID_now_present;
17926a79
DH
245 }
246
17926a79
DH
247 rxrpc_get_call(call);
248
249 rb_link_node(&call->sock_node, parent, pp);
250 rb_insert_color(&call->sock_node, &rx->calls);
251 write_unlock(&rx->call_lock);
252
253 write_lock_bh(&rxrpc_call_lock);
254 list_add_tail(&call->link, &rxrpc_calls);
255 write_unlock_bh(&rxrpc_call_lock);
256
aa390bbe 257 ret = rxrpc_begin_client_call(call, cp, srx, gfp);
999b69f8
DH
258 if (ret < 0)
259 goto error;
260
17926a79
DH
261 _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
262
263 _leave(" = %p [new]", call);
264 return call;
265
999b69f8
DH
266error:
267 write_lock(&rx->call_lock);
268 rb_erase(&call->sock_node, &rx->calls);
269 write_unlock(&rx->call_lock);
270 rxrpc_put_call(call);
271
272 write_lock_bh(&rxrpc_call_lock);
d1e858c5 273 list_del_init(&call->link);
999b69f8
DH
274 write_unlock_bh(&rxrpc_call_lock);
275
17b963e3 276 set_bit(RXRPC_CALL_RELEASED, &call->flags);
d1e858c5 277 call->state = RXRPC_CALL_DEAD;
999b69f8
DH
278 rxrpc_put_call(call);
279 _leave(" = %d", ret);
280 return ERR_PTR(ret);
281
2341e077
DH
282 /* We unexpectedly found the user ID in the list after taking
283 * the call_lock. This shouldn't happen unless the user races
284 * with itself and tries to add the same user ID twice at the
285 * same time in different threads.
286 */
287found_user_ID_now_present:
17926a79 288 write_unlock(&rx->call_lock);
17b963e3 289 set_bit(RXRPC_CALL_RELEASED, &call->flags);
d1e858c5 290 call->state = RXRPC_CALL_DEAD;
2341e077
DH
291 rxrpc_put_call(call);
292 _leave(" = -EEXIST [%p]", call);
293 return ERR_PTR(-EEXIST);
17926a79
DH
294}
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,
42886ffe 302 struct sk_buff *skb)
17926a79 303{
42886ffe 304 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 305 struct rxrpc_call *call, *candidate;
a1399f8b 306 u32 call_id, chan;
17926a79 307
843099ca 308 _enter(",%d", conn->debug_id);
17926a79
DH
309
310 ASSERT(rx != NULL);
311
843099ca 312 candidate = rxrpc_alloc_call(GFP_NOIO);
17926a79
DH
313 if (!candidate)
314 return ERR_PTR(-EBUSY);
315
a1399f8b 316 chan = sp->hdr.cid & RXRPC_CHANNELMASK;
42886ffe
DH
317 candidate->socket = rx;
318 candidate->conn = conn;
df5d8bf7 319 candidate->peer = conn->params.peer;
42886ffe
DH
320 candidate->cid = sp->hdr.cid;
321 candidate->call_id = sp->hdr.callNumber;
42886ffe
DH
322 candidate->rx_data_post = 0;
323 candidate->state = RXRPC_CALL_SERVER_ACCEPTING;
dabe5a79 324 candidate->flags |= (1 << RXRPC_CALL_IS_SERVICE);
17926a79
DH
325 if (conn->security_ix > 0)
326 candidate->state = RXRPC_CALL_SERVER_SECURING;
327
a1399f8b 328 spin_lock(&conn->channel_lock);
17926a79
DH
329
330 /* set the channel for this call */
a1399f8b
DH
331 call = rcu_dereference_protected(conn->channels[chan].call,
332 lockdep_is_held(&conn->channel_lock));
333
01a90a45 334 _debug("channel[%u] is %p", candidate->cid & RXRPC_CHANNELMASK, call);
42886ffe 335 if (call && call->call_id == sp->hdr.callNumber) {
17926a79
DH
336 /* already set; must've been a duplicate packet */
337 _debug("extant call [%d]", call->state);
338 ASSERTCMP(call->conn, ==, conn);
339
340 read_lock(&call->state_lock);
341 switch (call->state) {
342 case RXRPC_CALL_LOCALLY_ABORTED:
4c198ad1 343 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
651350d1 344 rxrpc_queue_call(call);
17926a79
DH
345 case RXRPC_CALL_REMOTELY_ABORTED:
346 read_unlock(&call->state_lock);
347 goto aborted_call;
348 default:
349 rxrpc_get_call(call);
350 read_unlock(&call->state_lock);
351 goto extant_call;
352 }
353 }
354
355 if (call) {
356 /* it seems the channel is still in use from the previous call
357 * - ditch the old binding if its call is now complete */
358 _debug("CALL: %u { %s }",
359 call->debug_id, rxrpc_call_states[call->state]);
360
361 if (call->state >= RXRPC_CALL_COMPLETE) {
45025bce 362 __rxrpc_disconnect_call(conn, call);
17926a79 363 } else {
a1399f8b 364 spin_unlock(&conn->channel_lock);
17926a79
DH
365 kmem_cache_free(rxrpc_call_jar, candidate);
366 _leave(" = -EBUSY");
367 return ERR_PTR(-EBUSY);
368 }
369 }
370
371 /* check the call number isn't duplicate */
372 _debug("check dup");
42886ffe 373 call_id = sp->hdr.callNumber;
a1399f8b
DH
374
375 /* We just ignore calls prior to the current call ID. Terminated calls
376 * are handled via the connection.
377 */
378 if (call_id <= conn->channels[chan].call_counter)
379 goto old_call; /* TODO: Just drop packet */
17926a79
DH
380
381 /* make the call available */
382 _debug("new call");
383 call = candidate;
384 candidate = NULL;
a1399f8b
DH
385 conn->channels[chan].call_counter = call_id;
386 rcu_assign_pointer(conn->channels[chan].call, call);
17926a79 387 sock_hold(&rx->sk);
5627cc8b 388 rxrpc_get_connection(conn);
df5d8bf7 389 rxrpc_get_peer(call->peer);
a1399f8b 390 spin_unlock(&conn->channel_lock);
17926a79 391
85f32278
DH
392 spin_lock(&conn->params.peer->lock);
393 hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
394 spin_unlock(&conn->params.peer->lock);
17926a79
DH
395
396 write_lock_bh(&rxrpc_call_lock);
397 list_add_tail(&call->link, &rxrpc_calls);
398 write_unlock_bh(&rxrpc_call_lock);
399
19ffa01c 400 call->service_id = conn->params.service_id;
7727640c 401
17926a79
DH
402 _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
403
5873c083 404 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
17926a79
DH
405 add_timer(&call->lifetimer);
406 _leave(" = %p {%d} [new]", call, call->debug_id);
407 return call;
408
409extant_call:
a1399f8b 410 spin_unlock(&conn->channel_lock);
17926a79
DH
411 kmem_cache_free(rxrpc_call_jar, candidate);
412 _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
413 return call;
414
415aborted_call:
a1399f8b 416 spin_unlock(&conn->channel_lock);
17926a79
DH
417 kmem_cache_free(rxrpc_call_jar, candidate);
418 _leave(" = -ECONNABORTED");
419 return ERR_PTR(-ECONNABORTED);
420
421old_call:
a1399f8b 422 spin_unlock(&conn->channel_lock);
17926a79
DH
423 kmem_cache_free(rxrpc_call_jar, candidate);
424 _leave(" = -ECONNRESET [old]");
425 return ERR_PTR(-ECONNRESET);
426}
427
17926a79
DH
428/*
429 * detach a call from a socket and set up for release
430 */
431void rxrpc_release_call(struct rxrpc_call *call)
432{
651350d1 433 struct rxrpc_connection *conn = call->conn;
17926a79
DH
434 struct rxrpc_sock *rx = call->socket;
435
436 _enter("{%d,%d,%d,%d}",
437 call->debug_id, atomic_read(&call->usage),
438 atomic_read(&call->ackr_not_idle),
439 call->rx_first_oos);
440
441 spin_lock_bh(&call->lock);
442 if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
443 BUG();
444 spin_unlock_bh(&call->lock);
445
446 /* dissociate from the socket
447 * - the socket's ref on the call is passed to the death timer
448 */
651350d1 449 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
17926a79 450
e653cfe4
DH
451 spin_lock(&conn->params.peer->lock);
452 hlist_del_init(&call->error_link);
453 spin_unlock(&conn->params.peer->lock);
454
17926a79
DH
455 write_lock_bh(&rx->call_lock);
456 if (!list_empty(&call->accept_link)) {
457 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
458 call, call->events, call->flags);
459 ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
460 list_del_init(&call->accept_link);
461 sk_acceptq_removed(&rx->sk);
462 } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
463 rb_erase(&call->sock_node, &rx->calls);
464 memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
465 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
466 }
467 write_unlock_bh(&rx->call_lock);
468
17926a79 469 /* free up the channel for reuse */
a1399f8b 470 write_lock_bh(&call->state_lock);
651350d1 471
17926a79
DH
472 if (call->state < RXRPC_CALL_COMPLETE &&
473 call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
474 _debug("+++ ABORTING STATE %d +++\n", call->state);
475 call->state = RXRPC_CALL_LOCALLY_ABORTED;
dc44b3a0 476 call->local_abort = RX_CALL_DEAD;
17926a79 477 }
a1399f8b 478 write_unlock_bh(&call->state_lock);
17926a79 479
e653cfe4
DH
480 rxrpc_disconnect_call(call);
481
651350d1 482 /* clean up the Rx queue */
17926a79
DH
483 if (!skb_queue_empty(&call->rx_queue) ||
484 !skb_queue_empty(&call->rx_oos_queue)) {
485 struct rxrpc_skb_priv *sp;
486 struct sk_buff *skb;
487
488 _debug("purge Rx queues");
489
490 spin_lock_bh(&call->lock);
491 while ((skb = skb_dequeue(&call->rx_queue)) ||
492 (skb = skb_dequeue(&call->rx_oos_queue))) {
17926a79
DH
493 spin_unlock_bh(&call->lock);
494
55cae7a4 495 sp = rxrpc_skb(skb);
17926a79
DH
496 _debug("- zap %s %%%u #%u",
497 rxrpc_pkts[sp->hdr.type],
0d12f8a4 498 sp->hdr.serial, sp->hdr.seq);
17926a79
DH
499 rxrpc_free_skb(skb);
500 spin_lock_bh(&call->lock);
501 }
502 spin_unlock_bh(&call->lock);
503
504 ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
505 }
506
507 del_timer_sync(&call->resend_timer);
508 del_timer_sync(&call->ack_timer);
509 del_timer_sync(&call->lifetimer);
5873c083 510 call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
17926a79
DH
511 add_timer(&call->deadspan);
512
513 _leave("");
514}
515
516/*
517 * handle a dead call being ready for reaping
518 */
519static void rxrpc_dead_call_expired(unsigned long _call)
520{
521 struct rxrpc_call *call = (struct rxrpc_call *) _call;
522
523 _enter("{%d}", call->debug_id);
524
525 write_lock_bh(&call->state_lock);
526 call->state = RXRPC_CALL_DEAD;
527 write_unlock_bh(&call->state_lock);
528 rxrpc_put_call(call);
529}
530
531/*
532 * mark a call as to be released, aborting it if it's still in progress
533 * - called with softirqs disabled
534 */
535static void rxrpc_mark_call_released(struct rxrpc_call *call)
536{
537 bool sched;
538
539 write_lock(&call->state_lock);
540 if (call->state < RXRPC_CALL_DEAD) {
541 sched = false;
542 if (call->state < RXRPC_CALL_COMPLETE) {
543 _debug("abort call %p", call);
544 call->state = RXRPC_CALL_LOCALLY_ABORTED;
dc44b3a0 545 call->local_abort = RX_CALL_DEAD;
4c198ad1 546 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
17926a79
DH
547 sched = true;
548 }
4c198ad1 549 if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
17926a79
DH
550 sched = true;
551 if (sched)
651350d1 552 rxrpc_queue_call(call);
17926a79
DH
553 }
554 write_unlock(&call->state_lock);
555}
556
557/*
558 * release all the calls associated with a socket
559 */
560void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
561{
562 struct rxrpc_call *call;
563 struct rb_node *p;
564
565 _enter("%p", rx);
566
567 read_lock_bh(&rx->call_lock);
568
17926a79
DH
569 /* kill the not-yet-accepted incoming calls */
570 list_for_each_entry(call, &rx->secureq, accept_link) {
571 rxrpc_mark_call_released(call);
572 }
573
574 list_for_each_entry(call, &rx->acceptq, accept_link) {
575 rxrpc_mark_call_released(call);
576 }
577
f36b5e44
DH
578 /* mark all the calls as no longer wanting incoming packets */
579 for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
580 call = rb_entry(p, struct rxrpc_call, sock_node);
581 rxrpc_mark_call_released(call);
582 }
583
17926a79
DH
584 read_unlock_bh(&rx->call_lock);
585 _leave("");
586}
587
588/*
589 * release a call
590 */
591void __rxrpc_put_call(struct rxrpc_call *call)
592{
593 ASSERT(call != NULL);
594
595 _enter("%p{u=%d}", call, atomic_read(&call->usage));
596
597 ASSERTCMP(atomic_read(&call->usage), >, 0);
598
599 if (atomic_dec_and_test(&call->usage)) {
600 _debug("call %d dead", call->debug_id);
372ee163 601 WARN_ON(atomic_read(&call->skb_count) != 0);
17926a79 602 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
651350d1 603 rxrpc_queue_work(&call->destroyer);
17926a79
DH
604 }
605 _leave("");
606}
607
dee46364
DH
608/*
609 * Final call destruction under RCU.
610 */
611static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
612{
613 struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
614
615 rxrpc_purge_queue(&call->rx_queue);
df5d8bf7 616 rxrpc_put_peer(call->peer);
dee46364
DH
617 kmem_cache_free(rxrpc_call_jar, call);
618}
619
17926a79
DH
620/*
621 * clean up a call
622 */
623static void rxrpc_cleanup_call(struct rxrpc_call *call)
624{
625 _net("DESTROY CALL %d", call->debug_id);
626
627 ASSERT(call->socket);
628
629 memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
630
631 del_timer_sync(&call->lifetimer);
632 del_timer_sync(&call->deadspan);
633 del_timer_sync(&call->ack_timer);
634 del_timer_sync(&call->resend_timer);
635
636 ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
637 ASSERTCMP(call->events, ==, 0);
638 if (work_pending(&call->processor)) {
639 _debug("defer destroy");
651350d1 640 rxrpc_queue_work(&call->destroyer);
17926a79
DH
641 return;
642 }
643
e653cfe4 644 ASSERTCMP(call->conn, ==, NULL);
17926a79
DH
645
646 if (call->acks_window) {
647 _debug("kill Tx window %d",
648 CIRC_CNT(call->acks_head, call->acks_tail,
649 call->acks_winsz));
650 smp_mb();
651 while (CIRC_CNT(call->acks_head, call->acks_tail,
652 call->acks_winsz) > 0) {
653 struct rxrpc_skb_priv *sp;
654 unsigned long _skb;
655
656 _skb = call->acks_window[call->acks_tail] & ~1;
0d12f8a4
DH
657 sp = rxrpc_skb((struct sk_buff *)_skb);
658 _debug("+++ clear Tx %u", sp->hdr.seq);
659 rxrpc_free_skb((struct sk_buff *)_skb);
17926a79
DH
660 call->acks_tail =
661 (call->acks_tail + 1) & (call->acks_winsz - 1);
662 }
663
664 kfree(call->acks_window);
665 }
666
667 rxrpc_free_skb(call->tx_pending);
668
669 rxrpc_purge_queue(&call->rx_queue);
670 ASSERT(skb_queue_empty(&call->rx_oos_queue));
671 sock_put(&call->socket->sk);
dee46364 672 call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
17926a79
DH
673}
674
675/*
676 * destroy a call
677 */
678static void rxrpc_destroy_call(struct work_struct *work)
679{
680 struct rxrpc_call *call =
681 container_of(work, struct rxrpc_call, destroyer);
682
01a90a45
DH
683 _enter("%p{%d,%x,%p}",
684 call, atomic_read(&call->usage), call->cid, call->conn);
17926a79
DH
685
686 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
687
688 write_lock_bh(&rxrpc_call_lock);
689 list_del_init(&call->link);
690 write_unlock_bh(&rxrpc_call_lock);
691
692 rxrpc_cleanup_call(call);
693 _leave("");
694}
695
696/*
697 * preemptively destroy all the call records from a transport endpoint rather
698 * than waiting for them to time out
699 */
700void __exit rxrpc_destroy_all_calls(void)
701{
702 struct rxrpc_call *call;
703
704 _enter("");
705 write_lock_bh(&rxrpc_call_lock);
706
707 while (!list_empty(&rxrpc_calls)) {
708 call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
709 _debug("Zapping call %p", call);
710
711 list_del_init(&call->link);
712
713 switch (atomic_read(&call->usage)) {
714 case 0:
715 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
716 break;
717 case 1:
718 if (del_timer_sync(&call->deadspan) != 0 &&
719 call->state != RXRPC_CALL_DEAD)
720 rxrpc_dead_call_expired((unsigned long) call);
721 if (call->state != RXRPC_CALL_DEAD)
722 break;
723 default:
9b6d5398 724 pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
17926a79
DH
725 call, atomic_read(&call->usage),
726 atomic_read(&call->ackr_not_idle),
727 rxrpc_call_states[call->state],
728 call->flags, call->events);
729 if (!skb_queue_empty(&call->rx_queue))
9b6d5398 730 pr_err("Rx queue occupied\n");
17926a79 731 if (!skb_queue_empty(&call->rx_oos_queue))
9b6d5398 732 pr_err("OOS queue occupied\n");
17926a79
DH
733 break;
734 }
735
736 write_unlock_bh(&rxrpc_call_lock);
737 cond_resched();
738 write_lock_bh(&rxrpc_call_lock);
739 }
740
741 write_unlock_bh(&rxrpc_call_lock);
742 _leave("");
743}
744
745/*
746 * handle call lifetime being exceeded
747 */
748static void rxrpc_call_life_expired(unsigned long _call)
749{
750 struct rxrpc_call *call = (struct rxrpc_call *) _call;
751
752 if (call->state >= RXRPC_CALL_COMPLETE)
753 return;
754
755 _enter("{%d}", call->debug_id);
756 read_lock_bh(&call->state_lock);
757 if (call->state < RXRPC_CALL_COMPLETE) {
4c198ad1 758 set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
651350d1 759 rxrpc_queue_call(call);
17926a79
DH
760 }
761 read_unlock_bh(&call->state_lock);
762}
763
764/*
765 * handle resend timer expiry
3b5bac2b 766 * - may not take call->state_lock as this can deadlock against del_timer_sync()
17926a79
DH
767 */
768static void rxrpc_resend_time_expired(unsigned long _call)
769{
770 struct rxrpc_call *call = (struct rxrpc_call *) _call;
771
772 _enter("{%d}", call->debug_id);
773
774 if (call->state >= RXRPC_CALL_COMPLETE)
775 return;
776
17926a79 777 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
4c198ad1 778 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
651350d1 779 rxrpc_queue_call(call);
17926a79
DH
780}
781
782/*
783 * handle ACK timer expiry
784 */
785static void rxrpc_ack_time_expired(unsigned long _call)
786{
787 struct rxrpc_call *call = (struct rxrpc_call *) _call;
788
789 _enter("{%d}", call->debug_id);
790
791 if (call->state >= RXRPC_CALL_COMPLETE)
792 return;
793
794 read_lock_bh(&call->state_lock);
795 if (call->state < RXRPC_CALL_COMPLETE &&
4c198ad1 796 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
651350d1 797 rxrpc_queue_call(call);
17926a79
DH
798 read_unlock_bh(&call->state_lock);
799}