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