Commit | Line | Data |
---|---|---|
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 | ||
5a0e3ad6 | 12 | #include <linux/slab.h> |
17926a79 DH |
13 | #include <linux/module.h> |
14 | #include <linux/circ_buf.h> | |
15 | #include <net/sock.h> | |
16 | #include <net/af_rxrpc.h> | |
17 | #include "ar-internal.h" | |
18 | ||
5873c083 DH |
19 | /* |
20 | * Maximum lifetime of a call (in jiffies). | |
21 | */ | |
22 | unsigned rxrpc_max_call_lifetime = 60 * HZ; | |
23 | ||
24 | /* | |
25 | * Time till dead call expires after last use (in jiffies). | |
26 | */ | |
27 | unsigned rxrpc_dead_call_expiry = 2 * HZ; | |
28 | ||
036c2e27 | 29 | const char *const rxrpc_call_states[] = { |
1f8481d1 DH |
30 | [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", |
31 | [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", | |
32 | [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", | |
33 | [RXRPC_CALL_CLIENT_FINAL_ACK] = "ClFnlACK", | |
34 | [RXRPC_CALL_SERVER_SECURING] = "SvSecure", | |
35 | [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept", | |
36 | [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", | |
37 | [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", | |
38 | [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", | |
39 | [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", | |
40 | [RXRPC_CALL_COMPLETE] = "Complete", | |
41 | [RXRPC_CALL_SERVER_BUSY] = "SvBusy ", | |
42 | [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", | |
43 | [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", | |
44 | [RXRPC_CALL_NETWORK_ERROR] = "NetError", | |
45 | [RXRPC_CALL_DEAD] = "Dead ", | |
46 | }; | |
47 | ||
17926a79 DH |
48 | struct kmem_cache *rxrpc_call_jar; |
49 | LIST_HEAD(rxrpc_calls); | |
50 | DEFINE_RWLOCK(rxrpc_call_lock); | |
17926a79 DH |
51 | |
52 | static void rxrpc_destroy_call(struct work_struct *work); | |
53 | static void rxrpc_call_life_expired(unsigned long _call); | |
54 | static void rxrpc_dead_call_expired(unsigned long _call); | |
55 | static void rxrpc_ack_time_expired(unsigned long _call); | |
56 | static void rxrpc_resend_time_expired(unsigned long _call); | |
57 | ||
58 | /* | |
59 | * allocate a new call | |
60 | */ | |
61 | static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp) | |
62 | { | |
63 | struct rxrpc_call *call; | |
64 | ||
65 | call = kmem_cache_zalloc(rxrpc_call_jar, gfp); | |
66 | if (!call) | |
67 | return NULL; | |
68 | ||
69 | call->acks_winsz = 16; | |
70 | call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long), | |
71 | gfp); | |
72 | if (!call->acks_window) { | |
73 | kmem_cache_free(rxrpc_call_jar, call); | |
74 | return NULL; | |
75 | } | |
76 | ||
77 | setup_timer(&call->lifetimer, &rxrpc_call_life_expired, | |
78 | (unsigned long) call); | |
79 | setup_timer(&call->deadspan, &rxrpc_dead_call_expired, | |
80 | (unsigned long) call); | |
81 | setup_timer(&call->ack_timer, &rxrpc_ack_time_expired, | |
82 | (unsigned long) call); | |
83 | setup_timer(&call->resend_timer, &rxrpc_resend_time_expired, | |
84 | (unsigned long) call); | |
85 | INIT_WORK(&call->destroyer, &rxrpc_destroy_call); | |
86 | INIT_WORK(&call->processor, &rxrpc_process_call); | |
87 | INIT_LIST_HEAD(&call->accept_link); | |
88 | skb_queue_head_init(&call->rx_queue); | |
89 | skb_queue_head_init(&call->rx_oos_queue); | |
90 | init_waitqueue_head(&call->tx_waitq); | |
91 | spin_lock_init(&call->lock); | |
92 | rwlock_init(&call->state_lock); | |
93 | atomic_set(&call->usage, 1); | |
94 | call->debug_id = atomic_inc_return(&rxrpc_debug_id); | |
95 | call->state = RXRPC_CALL_CLIENT_SEND_REQUEST; | |
96 | ||
97 | memset(&call->sock_node, 0xed, sizeof(call->sock_node)); | |
98 | ||
99 | call->rx_data_expect = 1; | |
100 | call->rx_data_eaten = 0; | |
101 | call->rx_first_oos = 0; | |
817913d8 | 102 | call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size; |
17926a79 DH |
103 | call->creation_jif = jiffies; |
104 | return call; | |
105 | } | |
106 | ||
107 | /* | |
fd589a8f | 108 | * allocate a new client call and attempt to get a connection slot for it |
17926a79 DH |
109 | */ |
110 | static struct rxrpc_call *rxrpc_alloc_client_call( | |
111 | struct rxrpc_sock *rx, | |
112 | struct rxrpc_transport *trans, | |
113 | struct rxrpc_conn_bundle *bundle, | |
114 | gfp_t gfp) | |
115 | { | |
116 | struct rxrpc_call *call; | |
117 | int ret; | |
118 | ||
119 | _enter(""); | |
120 | ||
121 | ASSERT(rx != NULL); | |
122 | ASSERT(trans != NULL); | |
123 | ASSERT(bundle != NULL); | |
124 | ||
125 | call = rxrpc_alloc_call(gfp); | |
126 | if (!call) | |
127 | return ERR_PTR(-ENOMEM); | |
128 | ||
129 | sock_hold(&rx->sk); | |
130 | call->socket = rx; | |
131 | call->rx_data_post = 1; | |
132 | ||
133 | ret = rxrpc_connect_call(rx, trans, bundle, call, gfp); | |
134 | if (ret < 0) { | |
135 | kmem_cache_free(rxrpc_call_jar, call); | |
136 | return ERR_PTR(ret); | |
137 | } | |
138 | ||
139 | spin_lock(&call->conn->trans->peer->lock); | |
140 | list_add(&call->error_link, &call->conn->trans->peer->error_targets); | |
141 | spin_unlock(&call->conn->trans->peer->lock); | |
142 | ||
5873c083 | 143 | call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime; |
17926a79 DH |
144 | add_timer(&call->lifetimer); |
145 | ||
146 | _leave(" = %p", call); | |
147 | return call; | |
148 | } | |
149 | ||
150 | /* | |
151 | * set up a call for the given data | |
152 | * - called in process context with IRQs enabled | |
153 | */ | |
154 | struct rxrpc_call *rxrpc_get_client_call(struct rxrpc_sock *rx, | |
155 | struct rxrpc_transport *trans, | |
156 | struct rxrpc_conn_bundle *bundle, | |
157 | unsigned long user_call_ID, | |
158 | int create, | |
159 | gfp_t gfp) | |
160 | { | |
161 | struct rxrpc_call *call, *candidate; | |
162 | struct rb_node *p, *parent, **pp; | |
163 | ||
164 | _enter("%p,%d,%d,%lx,%d", | |
165 | rx, trans ? trans->debug_id : -1, bundle ? bundle->debug_id : -1, | |
166 | user_call_ID, create); | |
167 | ||
168 | /* search the extant calls first for one that matches the specified | |
169 | * user ID */ | |
170 | read_lock(&rx->call_lock); | |
171 | ||
172 | p = rx->calls.rb_node; | |
173 | while (p) { | |
174 | call = rb_entry(p, struct rxrpc_call, sock_node); | |
175 | ||
176 | if (user_call_ID < call->user_call_ID) | |
177 | p = p->rb_left; | |
178 | else if (user_call_ID > call->user_call_ID) | |
179 | p = p->rb_right; | |
180 | else | |
181 | goto found_extant_call; | |
182 | } | |
183 | ||
184 | read_unlock(&rx->call_lock); | |
185 | ||
186 | if (!create || !trans) | |
187 | return ERR_PTR(-EBADSLT); | |
188 | ||
189 | /* not yet present - create a candidate for a new record and then | |
190 | * redo the search */ | |
191 | candidate = rxrpc_alloc_client_call(rx, trans, bundle, gfp); | |
192 | if (IS_ERR(candidate)) { | |
193 | _leave(" = %ld", PTR_ERR(candidate)); | |
194 | return candidate; | |
195 | } | |
196 | ||
197 | candidate->user_call_ID = user_call_ID; | |
198 | __set_bit(RXRPC_CALL_HAS_USERID, &candidate->flags); | |
199 | ||
200 | write_lock(&rx->call_lock); | |
201 | ||
202 | pp = &rx->calls.rb_node; | |
203 | parent = NULL; | |
204 | while (*pp) { | |
205 | parent = *pp; | |
206 | call = rb_entry(parent, struct rxrpc_call, sock_node); | |
207 | ||
208 | if (user_call_ID < call->user_call_ID) | |
209 | pp = &(*pp)->rb_left; | |
210 | else if (user_call_ID > call->user_call_ID) | |
211 | pp = &(*pp)->rb_right; | |
212 | else | |
213 | goto found_extant_second; | |
214 | } | |
215 | ||
216 | /* second search also failed; add the new call */ | |
217 | call = candidate; | |
218 | candidate = NULL; | |
219 | rxrpc_get_call(call); | |
220 | ||
221 | rb_link_node(&call->sock_node, parent, pp); | |
222 | rb_insert_color(&call->sock_node, &rx->calls); | |
223 | write_unlock(&rx->call_lock); | |
224 | ||
225 | write_lock_bh(&rxrpc_call_lock); | |
226 | list_add_tail(&call->link, &rxrpc_calls); | |
227 | write_unlock_bh(&rxrpc_call_lock); | |
228 | ||
229 | _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); | |
230 | ||
231 | _leave(" = %p [new]", call); | |
232 | return call; | |
233 | ||
234 | /* we found the call in the list immediately */ | |
235 | found_extant_call: | |
236 | rxrpc_get_call(call); | |
237 | read_unlock(&rx->call_lock); | |
238 | _leave(" = %p [extant %d]", call, atomic_read(&call->usage)); | |
239 | return call; | |
240 | ||
241 | /* we found the call on the second time through the list */ | |
242 | found_extant_second: | |
243 | rxrpc_get_call(call); | |
244 | write_unlock(&rx->call_lock); | |
245 | rxrpc_put_call(candidate); | |
246 | _leave(" = %p [second %d]", call, atomic_read(&call->usage)); | |
247 | return call; | |
248 | } | |
249 | ||
250 | /* | |
251 | * set up an incoming call | |
252 | * - called in process context with IRQs enabled | |
253 | */ | |
254 | struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx, | |
255 | struct rxrpc_connection *conn, | |
256 | struct rxrpc_header *hdr, | |
257 | gfp_t gfp) | |
258 | { | |
259 | struct rxrpc_call *call, *candidate; | |
260 | struct rb_node **p, *parent; | |
261 | __be32 call_id; | |
262 | ||
263 | _enter(",%d,,%x", conn->debug_id, gfp); | |
264 | ||
265 | ASSERT(rx != NULL); | |
266 | ||
267 | candidate = rxrpc_alloc_call(gfp); | |
268 | if (!candidate) | |
269 | return ERR_PTR(-EBUSY); | |
270 | ||
271 | candidate->socket = rx; | |
272 | candidate->conn = conn; | |
273 | candidate->cid = hdr->cid; | |
274 | candidate->call_id = hdr->callNumber; | |
275 | candidate->channel = ntohl(hdr->cid) & RXRPC_CHANNELMASK; | |
276 | candidate->rx_data_post = 0; | |
277 | candidate->state = RXRPC_CALL_SERVER_ACCEPTING; | |
278 | if (conn->security_ix > 0) | |
279 | candidate->state = RXRPC_CALL_SERVER_SECURING; | |
280 | ||
281 | write_lock_bh(&conn->lock); | |
282 | ||
283 | /* set the channel for this call */ | |
284 | call = conn->channels[candidate->channel]; | |
285 | _debug("channel[%u] is %p", candidate->channel, call); | |
286 | if (call && call->call_id == hdr->callNumber) { | |
287 | /* already set; must've been a duplicate packet */ | |
288 | _debug("extant call [%d]", call->state); | |
289 | ASSERTCMP(call->conn, ==, conn); | |
290 | ||
291 | read_lock(&call->state_lock); | |
292 | switch (call->state) { | |
293 | case RXRPC_CALL_LOCALLY_ABORTED: | |
294 | if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events)) | |
651350d1 | 295 | rxrpc_queue_call(call); |
17926a79 DH |
296 | case RXRPC_CALL_REMOTELY_ABORTED: |
297 | read_unlock(&call->state_lock); | |
298 | goto aborted_call; | |
299 | default: | |
300 | rxrpc_get_call(call); | |
301 | read_unlock(&call->state_lock); | |
302 | goto extant_call; | |
303 | } | |
304 | } | |
305 | ||
306 | if (call) { | |
307 | /* it seems the channel is still in use from the previous call | |
308 | * - ditch the old binding if its call is now complete */ | |
309 | _debug("CALL: %u { %s }", | |
310 | call->debug_id, rxrpc_call_states[call->state]); | |
311 | ||
312 | if (call->state >= RXRPC_CALL_COMPLETE) { | |
313 | conn->channels[call->channel] = NULL; | |
314 | } else { | |
315 | write_unlock_bh(&conn->lock); | |
316 | kmem_cache_free(rxrpc_call_jar, candidate); | |
317 | _leave(" = -EBUSY"); | |
318 | return ERR_PTR(-EBUSY); | |
319 | } | |
320 | } | |
321 | ||
322 | /* check the call number isn't duplicate */ | |
323 | _debug("check dup"); | |
324 | call_id = hdr->callNumber; | |
325 | p = &conn->calls.rb_node; | |
326 | parent = NULL; | |
327 | while (*p) { | |
328 | parent = *p; | |
329 | call = rb_entry(parent, struct rxrpc_call, conn_node); | |
330 | ||
331 | if (call_id < call->call_id) | |
332 | p = &(*p)->rb_left; | |
333 | else if (call_id > call->call_id) | |
334 | p = &(*p)->rb_right; | |
335 | else | |
336 | goto old_call; | |
337 | } | |
338 | ||
339 | /* make the call available */ | |
340 | _debug("new call"); | |
341 | call = candidate; | |
342 | candidate = NULL; | |
343 | rb_link_node(&call->conn_node, parent, p); | |
344 | rb_insert_color(&call->conn_node, &conn->calls); | |
345 | conn->channels[call->channel] = call; | |
346 | sock_hold(&rx->sk); | |
347 | atomic_inc(&conn->usage); | |
348 | write_unlock_bh(&conn->lock); | |
349 | ||
350 | spin_lock(&conn->trans->peer->lock); | |
351 | list_add(&call->error_link, &conn->trans->peer->error_targets); | |
352 | spin_unlock(&conn->trans->peer->lock); | |
353 | ||
354 | write_lock_bh(&rxrpc_call_lock); | |
355 | list_add_tail(&call->link, &rxrpc_calls); | |
356 | write_unlock_bh(&rxrpc_call_lock); | |
357 | ||
358 | _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); | |
359 | ||
5873c083 | 360 | call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime; |
17926a79 DH |
361 | add_timer(&call->lifetimer); |
362 | _leave(" = %p {%d} [new]", call, call->debug_id); | |
363 | return call; | |
364 | ||
365 | extant_call: | |
366 | write_unlock_bh(&conn->lock); | |
367 | kmem_cache_free(rxrpc_call_jar, candidate); | |
368 | _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1); | |
369 | return call; | |
370 | ||
371 | aborted_call: | |
372 | write_unlock_bh(&conn->lock); | |
373 | kmem_cache_free(rxrpc_call_jar, candidate); | |
374 | _leave(" = -ECONNABORTED"); | |
375 | return ERR_PTR(-ECONNABORTED); | |
376 | ||
377 | old_call: | |
378 | write_unlock_bh(&conn->lock); | |
379 | kmem_cache_free(rxrpc_call_jar, candidate); | |
380 | _leave(" = -ECONNRESET [old]"); | |
381 | return ERR_PTR(-ECONNRESET); | |
382 | } | |
383 | ||
384 | /* | |
385 | * find an extant server call | |
386 | * - called in process context with IRQs enabled | |
387 | */ | |
388 | struct rxrpc_call *rxrpc_find_server_call(struct rxrpc_sock *rx, | |
389 | unsigned long user_call_ID) | |
390 | { | |
391 | struct rxrpc_call *call; | |
392 | struct rb_node *p; | |
393 | ||
394 | _enter("%p,%lx", rx, user_call_ID); | |
395 | ||
396 | /* search the extant calls for one that matches the specified user | |
397 | * ID */ | |
398 | read_lock(&rx->call_lock); | |
399 | ||
400 | p = rx->calls.rb_node; | |
401 | while (p) { | |
402 | call = rb_entry(p, struct rxrpc_call, sock_node); | |
403 | ||
404 | if (user_call_ID < call->user_call_ID) | |
405 | p = p->rb_left; | |
406 | else if (user_call_ID > call->user_call_ID) | |
407 | p = p->rb_right; | |
408 | else | |
409 | goto found_extant_call; | |
410 | } | |
411 | ||
412 | read_unlock(&rx->call_lock); | |
413 | _leave(" = NULL"); | |
414 | return NULL; | |
415 | ||
416 | /* we found the call in the list immediately */ | |
417 | found_extant_call: | |
418 | rxrpc_get_call(call); | |
419 | read_unlock(&rx->call_lock); | |
420 | _leave(" = %p [%d]", call, atomic_read(&call->usage)); | |
421 | return call; | |
422 | } | |
423 | ||
424 | /* | |
425 | * detach a call from a socket and set up for release | |
426 | */ | |
427 | void rxrpc_release_call(struct rxrpc_call *call) | |
428 | { | |
651350d1 | 429 | struct rxrpc_connection *conn = call->conn; |
17926a79 DH |
430 | struct rxrpc_sock *rx = call->socket; |
431 | ||
432 | _enter("{%d,%d,%d,%d}", | |
433 | call->debug_id, atomic_read(&call->usage), | |
434 | atomic_read(&call->ackr_not_idle), | |
435 | call->rx_first_oos); | |
436 | ||
437 | spin_lock_bh(&call->lock); | |
438 | if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) | |
439 | BUG(); | |
440 | spin_unlock_bh(&call->lock); | |
441 | ||
442 | /* dissociate from the socket | |
443 | * - the socket's ref on the call is passed to the death timer | |
444 | */ | |
651350d1 | 445 | _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); |
17926a79 DH |
446 | |
447 | write_lock_bh(&rx->call_lock); | |
448 | if (!list_empty(&call->accept_link)) { | |
449 | _debug("unlinking once-pending call %p { e=%lx f=%lx }", | |
450 | call, call->events, call->flags); | |
451 | ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags)); | |
452 | list_del_init(&call->accept_link); | |
453 | sk_acceptq_removed(&rx->sk); | |
454 | } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { | |
455 | rb_erase(&call->sock_node, &rx->calls); | |
456 | memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); | |
457 | clear_bit(RXRPC_CALL_HAS_USERID, &call->flags); | |
458 | } | |
459 | write_unlock_bh(&rx->call_lock); | |
460 | ||
17926a79 | 461 | /* free up the channel for reuse */ |
651350d1 DH |
462 | spin_lock(&conn->trans->client_lock); |
463 | write_lock_bh(&conn->lock); | |
464 | write_lock(&call->state_lock); | |
465 | ||
466 | if (conn->channels[call->channel] == call) | |
467 | conn->channels[call->channel] = NULL; | |
468 | ||
469 | if (conn->out_clientflag && conn->bundle) { | |
470 | conn->avail_calls++; | |
471 | switch (conn->avail_calls) { | |
472 | case 1: | |
473 | list_move_tail(&conn->bundle_link, | |
474 | &conn->bundle->avail_conns); | |
475 | case 2 ... RXRPC_MAXCALLS - 1: | |
476 | ASSERT(conn->channels[0] == NULL || | |
477 | conn->channels[1] == NULL || | |
478 | conn->channels[2] == NULL || | |
479 | conn->channels[3] == NULL); | |
480 | break; | |
481 | case RXRPC_MAXCALLS: | |
482 | list_move_tail(&conn->bundle_link, | |
483 | &conn->bundle->unused_conns); | |
484 | ASSERT(conn->channels[0] == NULL && | |
485 | conn->channels[1] == NULL && | |
486 | conn->channels[2] == NULL && | |
487 | conn->channels[3] == NULL); | |
488 | break; | |
489 | default: | |
490 | printk(KERN_ERR "RxRPC: conn->avail_calls=%d\n", | |
491 | conn->avail_calls); | |
492 | BUG(); | |
493 | } | |
17926a79 DH |
494 | } |
495 | ||
651350d1 | 496 | spin_unlock(&conn->trans->client_lock); |
17926a79 DH |
497 | |
498 | if (call->state < RXRPC_CALL_COMPLETE && | |
499 | call->state != RXRPC_CALL_CLIENT_FINAL_ACK) { | |
500 | _debug("+++ ABORTING STATE %d +++\n", call->state); | |
501 | call->state = RXRPC_CALL_LOCALLY_ABORTED; | |
502 | call->abort_code = RX_CALL_DEAD; | |
503 | set_bit(RXRPC_CALL_ABORT, &call->events); | |
651350d1 | 504 | rxrpc_queue_call(call); |
17926a79 DH |
505 | } |
506 | write_unlock(&call->state_lock); | |
651350d1 | 507 | write_unlock_bh(&conn->lock); |
17926a79 | 508 | |
651350d1 | 509 | /* clean up the Rx queue */ |
17926a79 DH |
510 | if (!skb_queue_empty(&call->rx_queue) || |
511 | !skb_queue_empty(&call->rx_oos_queue)) { | |
512 | struct rxrpc_skb_priv *sp; | |
513 | struct sk_buff *skb; | |
514 | ||
515 | _debug("purge Rx queues"); | |
516 | ||
517 | spin_lock_bh(&call->lock); | |
518 | while ((skb = skb_dequeue(&call->rx_queue)) || | |
519 | (skb = skb_dequeue(&call->rx_oos_queue))) { | |
520 | sp = rxrpc_skb(skb); | |
521 | if (sp->call) { | |
522 | ASSERTCMP(sp->call, ==, call); | |
523 | rxrpc_put_call(call); | |
524 | sp->call = NULL; | |
525 | } | |
526 | skb->destructor = NULL; | |
527 | spin_unlock_bh(&call->lock); | |
528 | ||
529 | _debug("- zap %s %%%u #%u", | |
530 | rxrpc_pkts[sp->hdr.type], | |
531 | ntohl(sp->hdr.serial), | |
532 | ntohl(sp->hdr.seq)); | |
533 | rxrpc_free_skb(skb); | |
534 | spin_lock_bh(&call->lock); | |
535 | } | |
536 | spin_unlock_bh(&call->lock); | |
537 | ||
538 | ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE); | |
539 | } | |
540 | ||
541 | del_timer_sync(&call->resend_timer); | |
542 | del_timer_sync(&call->ack_timer); | |
543 | del_timer_sync(&call->lifetimer); | |
5873c083 | 544 | call->deadspan.expires = jiffies + rxrpc_dead_call_expiry; |
17926a79 DH |
545 | add_timer(&call->deadspan); |
546 | ||
547 | _leave(""); | |
548 | } | |
549 | ||
550 | /* | |
551 | * handle a dead call being ready for reaping | |
552 | */ | |
553 | static void rxrpc_dead_call_expired(unsigned long _call) | |
554 | { | |
555 | struct rxrpc_call *call = (struct rxrpc_call *) _call; | |
556 | ||
557 | _enter("{%d}", call->debug_id); | |
558 | ||
559 | write_lock_bh(&call->state_lock); | |
560 | call->state = RXRPC_CALL_DEAD; | |
561 | write_unlock_bh(&call->state_lock); | |
562 | rxrpc_put_call(call); | |
563 | } | |
564 | ||
565 | /* | |
566 | * mark a call as to be released, aborting it if it's still in progress | |
567 | * - called with softirqs disabled | |
568 | */ | |
569 | static void rxrpc_mark_call_released(struct rxrpc_call *call) | |
570 | { | |
571 | bool sched; | |
572 | ||
573 | write_lock(&call->state_lock); | |
574 | if (call->state < RXRPC_CALL_DEAD) { | |
575 | sched = false; | |
576 | if (call->state < RXRPC_CALL_COMPLETE) { | |
577 | _debug("abort call %p", call); | |
578 | call->state = RXRPC_CALL_LOCALLY_ABORTED; | |
579 | call->abort_code = RX_CALL_DEAD; | |
580 | if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events)) | |
581 | sched = true; | |
582 | } | |
583 | if (!test_and_set_bit(RXRPC_CALL_RELEASE, &call->events)) | |
584 | sched = true; | |
585 | if (sched) | |
651350d1 | 586 | rxrpc_queue_call(call); |
17926a79 DH |
587 | } |
588 | write_unlock(&call->state_lock); | |
589 | } | |
590 | ||
591 | /* | |
592 | * release all the calls associated with a socket | |
593 | */ | |
594 | void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) | |
595 | { | |
596 | struct rxrpc_call *call; | |
597 | struct rb_node *p; | |
598 | ||
599 | _enter("%p", rx); | |
600 | ||
601 | read_lock_bh(&rx->call_lock); | |
602 | ||
603 | /* mark all the calls as no longer wanting incoming packets */ | |
604 | for (p = rb_first(&rx->calls); p; p = rb_next(p)) { | |
605 | call = rb_entry(p, struct rxrpc_call, sock_node); | |
606 | rxrpc_mark_call_released(call); | |
607 | } | |
608 | ||
609 | /* kill the not-yet-accepted incoming calls */ | |
610 | list_for_each_entry(call, &rx->secureq, accept_link) { | |
611 | rxrpc_mark_call_released(call); | |
612 | } | |
613 | ||
614 | list_for_each_entry(call, &rx->acceptq, accept_link) { | |
615 | rxrpc_mark_call_released(call); | |
616 | } | |
617 | ||
618 | read_unlock_bh(&rx->call_lock); | |
619 | _leave(""); | |
620 | } | |
621 | ||
622 | /* | |
623 | * release a call | |
624 | */ | |
625 | void __rxrpc_put_call(struct rxrpc_call *call) | |
626 | { | |
627 | ASSERT(call != NULL); | |
628 | ||
629 | _enter("%p{u=%d}", call, atomic_read(&call->usage)); | |
630 | ||
631 | ASSERTCMP(atomic_read(&call->usage), >, 0); | |
632 | ||
633 | if (atomic_dec_and_test(&call->usage)) { | |
634 | _debug("call %d dead", call->debug_id); | |
635 | ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD); | |
651350d1 | 636 | rxrpc_queue_work(&call->destroyer); |
17926a79 DH |
637 | } |
638 | _leave(""); | |
639 | } | |
640 | ||
641 | /* | |
642 | * clean up a call | |
643 | */ | |
644 | static void rxrpc_cleanup_call(struct rxrpc_call *call) | |
645 | { | |
646 | _net("DESTROY CALL %d", call->debug_id); | |
647 | ||
648 | ASSERT(call->socket); | |
649 | ||
650 | memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); | |
651 | ||
652 | del_timer_sync(&call->lifetimer); | |
653 | del_timer_sync(&call->deadspan); | |
654 | del_timer_sync(&call->ack_timer); | |
655 | del_timer_sync(&call->resend_timer); | |
656 | ||
657 | ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); | |
658 | ASSERTCMP(call->events, ==, 0); | |
659 | if (work_pending(&call->processor)) { | |
660 | _debug("defer destroy"); | |
651350d1 | 661 | rxrpc_queue_work(&call->destroyer); |
17926a79 DH |
662 | return; |
663 | } | |
664 | ||
665 | if (call->conn) { | |
666 | spin_lock(&call->conn->trans->peer->lock); | |
667 | list_del(&call->error_link); | |
668 | spin_unlock(&call->conn->trans->peer->lock); | |
669 | ||
670 | write_lock_bh(&call->conn->lock); | |
671 | rb_erase(&call->conn_node, &call->conn->calls); | |
672 | write_unlock_bh(&call->conn->lock); | |
673 | rxrpc_put_connection(call->conn); | |
674 | } | |
675 | ||
676 | if (call->acks_window) { | |
677 | _debug("kill Tx window %d", | |
678 | CIRC_CNT(call->acks_head, call->acks_tail, | |
679 | call->acks_winsz)); | |
680 | smp_mb(); | |
681 | while (CIRC_CNT(call->acks_head, call->acks_tail, | |
682 | call->acks_winsz) > 0) { | |
683 | struct rxrpc_skb_priv *sp; | |
684 | unsigned long _skb; | |
685 | ||
686 | _skb = call->acks_window[call->acks_tail] & ~1; | |
687 | sp = rxrpc_skb((struct sk_buff *) _skb); | |
688 | _debug("+++ clear Tx %u", ntohl(sp->hdr.seq)); | |
689 | rxrpc_free_skb((struct sk_buff *) _skb); | |
690 | call->acks_tail = | |
691 | (call->acks_tail + 1) & (call->acks_winsz - 1); | |
692 | } | |
693 | ||
694 | kfree(call->acks_window); | |
695 | } | |
696 | ||
697 | rxrpc_free_skb(call->tx_pending); | |
698 | ||
699 | rxrpc_purge_queue(&call->rx_queue); | |
700 | ASSERT(skb_queue_empty(&call->rx_oos_queue)); | |
701 | sock_put(&call->socket->sk); | |
702 | kmem_cache_free(rxrpc_call_jar, call); | |
703 | } | |
704 | ||
705 | /* | |
706 | * destroy a call | |
707 | */ | |
708 | static void rxrpc_destroy_call(struct work_struct *work) | |
709 | { | |
710 | struct rxrpc_call *call = | |
711 | container_of(work, struct rxrpc_call, destroyer); | |
712 | ||
713 | _enter("%p{%d,%d,%p}", | |
714 | call, atomic_read(&call->usage), call->channel, call->conn); | |
715 | ||
716 | ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD); | |
717 | ||
718 | write_lock_bh(&rxrpc_call_lock); | |
719 | list_del_init(&call->link); | |
720 | write_unlock_bh(&rxrpc_call_lock); | |
721 | ||
722 | rxrpc_cleanup_call(call); | |
723 | _leave(""); | |
724 | } | |
725 | ||
726 | /* | |
727 | * preemptively destroy all the call records from a transport endpoint rather | |
728 | * than waiting for them to time out | |
729 | */ | |
730 | void __exit rxrpc_destroy_all_calls(void) | |
731 | { | |
732 | struct rxrpc_call *call; | |
733 | ||
734 | _enter(""); | |
735 | write_lock_bh(&rxrpc_call_lock); | |
736 | ||
737 | while (!list_empty(&rxrpc_calls)) { | |
738 | call = list_entry(rxrpc_calls.next, struct rxrpc_call, link); | |
739 | _debug("Zapping call %p", call); | |
740 | ||
741 | list_del_init(&call->link); | |
742 | ||
743 | switch (atomic_read(&call->usage)) { | |
744 | case 0: | |
745 | ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD); | |
746 | break; | |
747 | case 1: | |
748 | if (del_timer_sync(&call->deadspan) != 0 && | |
749 | call->state != RXRPC_CALL_DEAD) | |
750 | rxrpc_dead_call_expired((unsigned long) call); | |
751 | if (call->state != RXRPC_CALL_DEAD) | |
752 | break; | |
753 | default: | |
754 | printk(KERN_ERR "RXRPC:" | |
755 | " Call %p still in use (%d,%d,%s,%lx,%lx)!\n", | |
756 | call, atomic_read(&call->usage), | |
757 | atomic_read(&call->ackr_not_idle), | |
758 | rxrpc_call_states[call->state], | |
759 | call->flags, call->events); | |
760 | if (!skb_queue_empty(&call->rx_queue)) | |
761 | printk(KERN_ERR"RXRPC: Rx queue occupied\n"); | |
762 | if (!skb_queue_empty(&call->rx_oos_queue)) | |
763 | printk(KERN_ERR"RXRPC: OOS queue occupied\n"); | |
764 | break; | |
765 | } | |
766 | ||
767 | write_unlock_bh(&rxrpc_call_lock); | |
768 | cond_resched(); | |
769 | write_lock_bh(&rxrpc_call_lock); | |
770 | } | |
771 | ||
772 | write_unlock_bh(&rxrpc_call_lock); | |
773 | _leave(""); | |
774 | } | |
775 | ||
776 | /* | |
777 | * handle call lifetime being exceeded | |
778 | */ | |
779 | static void rxrpc_call_life_expired(unsigned long _call) | |
780 | { | |
781 | struct rxrpc_call *call = (struct rxrpc_call *) _call; | |
782 | ||
783 | if (call->state >= RXRPC_CALL_COMPLETE) | |
784 | return; | |
785 | ||
786 | _enter("{%d}", call->debug_id); | |
787 | read_lock_bh(&call->state_lock); | |
788 | if (call->state < RXRPC_CALL_COMPLETE) { | |
789 | set_bit(RXRPC_CALL_LIFE_TIMER, &call->events); | |
651350d1 | 790 | rxrpc_queue_call(call); |
17926a79 DH |
791 | } |
792 | read_unlock_bh(&call->state_lock); | |
793 | } | |
794 | ||
795 | /* | |
796 | * handle resend timer expiry | |
3b5bac2b | 797 | * - may not take call->state_lock as this can deadlock against del_timer_sync() |
17926a79 DH |
798 | */ |
799 | static void rxrpc_resend_time_expired(unsigned long _call) | |
800 | { | |
801 | struct rxrpc_call *call = (struct rxrpc_call *) _call; | |
802 | ||
803 | _enter("{%d}", call->debug_id); | |
804 | ||
805 | if (call->state >= RXRPC_CALL_COMPLETE) | |
806 | return; | |
807 | ||
17926a79 | 808 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
3b5bac2b | 809 | if (!test_and_set_bit(RXRPC_CALL_RESEND_TIMER, &call->events)) |
651350d1 | 810 | rxrpc_queue_call(call); |
17926a79 DH |
811 | } |
812 | ||
813 | /* | |
814 | * handle ACK timer expiry | |
815 | */ | |
816 | static void rxrpc_ack_time_expired(unsigned long _call) | |
817 | { | |
818 | struct rxrpc_call *call = (struct rxrpc_call *) _call; | |
819 | ||
820 | _enter("{%d}", call->debug_id); | |
821 | ||
822 | if (call->state >= RXRPC_CALL_COMPLETE) | |
823 | return; | |
824 | ||
825 | read_lock_bh(&call->state_lock); | |
826 | if (call->state < RXRPC_CALL_COMPLETE && | |
827 | !test_and_set_bit(RXRPC_CALL_ACK, &call->events)) | |
651350d1 | 828 | rxrpc_queue_call(call); |
17926a79 DH |
829 | read_unlock_bh(&call->state_lock); |
830 | } |