ar-call.c 21 KB

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