call_object.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/circ_buf.h>
  15. #include <linux/spinlock_types.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include "ar-internal.h"
  19. const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
  20. [RXRPC_CALL_UNINITIALISED] = "Uninit ",
  21. [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn",
  22. [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq",
  23. [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl",
  24. [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl",
  25. [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc",
  26. [RXRPC_CALL_SERVER_SECURING] = "SvSecure",
  27. [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept",
  28. [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq",
  29. [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq",
  30. [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl",
  31. [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK",
  32. [RXRPC_CALL_COMPLETE] = "Complete",
  33. };
  34. const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
  35. [RXRPC_CALL_SUCCEEDED] = "Complete",
  36. [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort",
  37. [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort",
  38. [RXRPC_CALL_LOCAL_ERROR] = "LocError",
  39. [RXRPC_CALL_NETWORK_ERROR] = "NetError",
  40. };
  41. struct kmem_cache *rxrpc_call_jar;
  42. static void rxrpc_call_timer_expired(struct timer_list *t)
  43. {
  44. struct rxrpc_call *call = from_timer(call, t, timer);
  45. _enter("%d", call->debug_id);
  46. if (call->state < RXRPC_CALL_COMPLETE) {
  47. trace_rxrpc_timer(call, rxrpc_timer_expired, jiffies);
  48. rxrpc_queue_call(call);
  49. }
  50. }
  51. static struct lock_class_key rxrpc_call_user_mutex_lock_class_key;
  52. /*
  53. * find an extant server call
  54. * - called in process context with IRQs enabled
  55. */
  56. struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
  57. unsigned long user_call_ID)
  58. {
  59. struct rxrpc_call *call;
  60. struct rb_node *p;
  61. _enter("%p,%lx", rx, user_call_ID);
  62. read_lock(&rx->call_lock);
  63. p = rx->calls.rb_node;
  64. while (p) {
  65. call = rb_entry(p, struct rxrpc_call, sock_node);
  66. if (user_call_ID < call->user_call_ID)
  67. p = p->rb_left;
  68. else if (user_call_ID > call->user_call_ID)
  69. p = p->rb_right;
  70. else
  71. goto found_extant_call;
  72. }
  73. read_unlock(&rx->call_lock);
  74. _leave(" = NULL");
  75. return NULL;
  76. found_extant_call:
  77. rxrpc_get_call(call, rxrpc_call_got);
  78. read_unlock(&rx->call_lock);
  79. _leave(" = %p [%d]", call, atomic_read(&call->usage));
  80. return call;
  81. }
  82. /*
  83. * allocate a new call
  84. */
  85. struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp,
  86. unsigned int debug_id)
  87. {
  88. struct rxrpc_call *call;
  89. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  90. call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
  91. if (!call)
  92. return NULL;
  93. call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
  94. sizeof(struct sk_buff *),
  95. gfp);
  96. if (!call->rxtx_buffer)
  97. goto nomem;
  98. call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
  99. if (!call->rxtx_annotations)
  100. goto nomem_2;
  101. mutex_init(&call->user_mutex);
  102. /* Prevent lockdep reporting a deadlock false positive between the afs
  103. * filesystem and sys_sendmsg() via the mmap sem.
  104. */
  105. if (rx->sk.sk_kern_sock)
  106. lockdep_set_class(&call->user_mutex,
  107. &rxrpc_call_user_mutex_lock_class_key);
  108. timer_setup(&call->timer, rxrpc_call_timer_expired, 0);
  109. INIT_WORK(&call->processor, &rxrpc_process_call);
  110. INIT_LIST_HEAD(&call->link);
  111. INIT_LIST_HEAD(&call->chan_wait_link);
  112. INIT_LIST_HEAD(&call->accept_link);
  113. INIT_LIST_HEAD(&call->recvmsg_link);
  114. INIT_LIST_HEAD(&call->sock_link);
  115. init_waitqueue_head(&call->waitq);
  116. spin_lock_init(&call->lock);
  117. spin_lock_init(&call->notify_lock);
  118. rwlock_init(&call->state_lock);
  119. atomic_set(&call->usage, 1);
  120. call->debug_id = debug_id;
  121. call->tx_total_len = -1;
  122. call->next_rx_timo = 20 * HZ;
  123. call->next_req_timo = 1 * HZ;
  124. memset(&call->sock_node, 0xed, sizeof(call->sock_node));
  125. /* Leave space in the ring to handle a maxed-out jumbo packet */
  126. call->rx_winsize = rxrpc_rx_window_size;
  127. call->tx_winsize = 16;
  128. call->rx_expect_next = 1;
  129. call->cong_cwnd = 2;
  130. call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
  131. call->rxnet = rxnet;
  132. atomic_inc(&rxnet->nr_calls);
  133. return call;
  134. nomem_2:
  135. kfree(call->rxtx_buffer);
  136. nomem:
  137. kmem_cache_free(rxrpc_call_jar, call);
  138. return NULL;
  139. }
  140. /*
  141. * Allocate a new client call.
  142. */
  143. static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
  144. struct sockaddr_rxrpc *srx,
  145. gfp_t gfp,
  146. unsigned int debug_id)
  147. {
  148. struct rxrpc_call *call;
  149. ktime_t now;
  150. _enter("");
  151. call = rxrpc_alloc_call(rx, gfp, debug_id);
  152. if (!call)
  153. return ERR_PTR(-ENOMEM);
  154. call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
  155. call->service_id = srx->srx_service;
  156. call->tx_phase = true;
  157. now = ktime_get_real();
  158. call->acks_latest_ts = now;
  159. call->cong_tstamp = now;
  160. _leave(" = %p", call);
  161. return call;
  162. }
  163. /*
  164. * Initiate the call ack/resend/expiry timer.
  165. */
  166. static void rxrpc_start_call_timer(struct rxrpc_call *call)
  167. {
  168. unsigned long now = jiffies;
  169. unsigned long j = now + MAX_JIFFY_OFFSET;
  170. call->ack_at = j;
  171. call->ack_lost_at = j;
  172. call->resend_at = j;
  173. call->ping_at = j;
  174. call->expect_rx_by = j;
  175. call->expect_req_by = j;
  176. call->expect_term_by = j;
  177. call->timer.expires = now;
  178. }
  179. /*
  180. * Set up a call for the given parameters.
  181. * - Called with the socket lock held, which it must release.
  182. * - If it returns a call, the call's lock will need releasing by the caller.
  183. */
  184. struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
  185. struct rxrpc_conn_parameters *cp,
  186. struct sockaddr_rxrpc *srx,
  187. struct rxrpc_call_params *p,
  188. gfp_t gfp,
  189. unsigned int debug_id)
  190. __releases(&rx->sk.sk_lock.slock)
  191. __acquires(&call->user_mutex)
  192. {
  193. struct rxrpc_call *call, *xcall;
  194. struct rxrpc_net *rxnet;
  195. struct rb_node *parent, **pp;
  196. const void *here = __builtin_return_address(0);
  197. int ret;
  198. _enter("%p,%lx", rx, p->user_call_ID);
  199. call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id);
  200. if (IS_ERR(call)) {
  201. release_sock(&rx->sk);
  202. _leave(" = %ld", PTR_ERR(call));
  203. return call;
  204. }
  205. call->tx_total_len = p->tx_total_len;
  206. trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage),
  207. here, (const void *)p->user_call_ID);
  208. /* We need to protect a partially set up call against the user as we
  209. * will be acting outside the socket lock.
  210. */
  211. mutex_lock(&call->user_mutex);
  212. /* Publish the call, even though it is incompletely set up as yet */
  213. write_lock(&rx->call_lock);
  214. pp = &rx->calls.rb_node;
  215. parent = NULL;
  216. while (*pp) {
  217. parent = *pp;
  218. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  219. if (p->user_call_ID < xcall->user_call_ID)
  220. pp = &(*pp)->rb_left;
  221. else if (p->user_call_ID > xcall->user_call_ID)
  222. pp = &(*pp)->rb_right;
  223. else
  224. goto error_dup_user_ID;
  225. }
  226. rcu_assign_pointer(call->socket, rx);
  227. call->user_call_ID = p->user_call_ID;
  228. __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  229. rxrpc_get_call(call, rxrpc_call_got_userid);
  230. rb_link_node(&call->sock_node, parent, pp);
  231. rb_insert_color(&call->sock_node, &rx->calls);
  232. list_add(&call->sock_link, &rx->sock_calls);
  233. write_unlock(&rx->call_lock);
  234. rxnet = call->rxnet;
  235. write_lock(&rxnet->call_lock);
  236. list_add_tail(&call->link, &rxnet->calls);
  237. write_unlock(&rxnet->call_lock);
  238. /* From this point on, the call is protected by its own lock. */
  239. release_sock(&rx->sk);
  240. /* Set up or get a connection record and set the protocol parameters,
  241. * including channel number and call ID.
  242. */
  243. ret = rxrpc_connect_call(call, cp, srx, gfp);
  244. if (ret < 0)
  245. goto error;
  246. trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
  247. here, NULL);
  248. rxrpc_start_call_timer(call);
  249. _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
  250. _leave(" = %p [new]", call);
  251. return call;
  252. /* We unexpectedly found the user ID in the list after taking
  253. * the call_lock. This shouldn't happen unless the user races
  254. * with itself and tries to add the same user ID twice at the
  255. * same time in different threads.
  256. */
  257. error_dup_user_ID:
  258. write_unlock(&rx->call_lock);
  259. release_sock(&rx->sk);
  260. ret = -EEXIST;
  261. error:
  262. __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  263. RX_CALL_DEAD, ret);
  264. trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
  265. here, ERR_PTR(ret));
  266. rxrpc_release_call(rx, call);
  267. mutex_unlock(&call->user_mutex);
  268. rxrpc_put_call(call, rxrpc_call_put);
  269. _leave(" = %d", ret);
  270. return ERR_PTR(ret);
  271. }
  272. /*
  273. * Retry a call to a new address. It is expected that the Tx queue of the call
  274. * will contain data previously packaged for an old call.
  275. */
  276. int rxrpc_retry_client_call(struct rxrpc_sock *rx,
  277. struct rxrpc_call *call,
  278. struct rxrpc_conn_parameters *cp,
  279. struct sockaddr_rxrpc *srx,
  280. gfp_t gfp)
  281. {
  282. const void *here = __builtin_return_address(0);
  283. int ret;
  284. /* Set up or get a connection record and set the protocol parameters,
  285. * including channel number and call ID.
  286. */
  287. ret = rxrpc_connect_call(call, cp, srx, gfp);
  288. if (ret < 0)
  289. goto error;
  290. trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
  291. here, NULL);
  292. rxrpc_start_call_timer(call);
  293. _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
  294. if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  295. rxrpc_queue_call(call);
  296. _leave(" = 0");
  297. return 0;
  298. error:
  299. rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  300. RX_CALL_DEAD, ret);
  301. trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
  302. here, ERR_PTR(ret));
  303. _leave(" = %d", ret);
  304. return ret;
  305. }
  306. /*
  307. * Set up an incoming call. call->conn points to the connection.
  308. * This is called in BH context and isn't allowed to fail.
  309. */
  310. void rxrpc_incoming_call(struct rxrpc_sock *rx,
  311. struct rxrpc_call *call,
  312. struct sk_buff *skb)
  313. {
  314. struct rxrpc_connection *conn = call->conn;
  315. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  316. u32 chan;
  317. _enter(",%d", call->conn->debug_id);
  318. rcu_assign_pointer(call->socket, rx);
  319. call->call_id = sp->hdr.callNumber;
  320. call->service_id = sp->hdr.serviceId;
  321. call->cid = sp->hdr.cid;
  322. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  323. if (sp->hdr.securityIndex > 0)
  324. call->state = RXRPC_CALL_SERVER_SECURING;
  325. call->cong_tstamp = skb->tstamp;
  326. /* Set the channel for this call. We don't get channel_lock as we're
  327. * only defending against the data_ready handler (which we're called
  328. * from) and the RESPONSE packet parser (which is only really
  329. * interested in call_counter and can cope with a disagreement with the
  330. * call pointer).
  331. */
  332. chan = sp->hdr.cid & RXRPC_CHANNELMASK;
  333. conn->channels[chan].call_counter = call->call_id;
  334. conn->channels[chan].call_id = call->call_id;
  335. rcu_assign_pointer(conn->channels[chan].call, call);
  336. spin_lock(&conn->params.peer->lock);
  337. hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
  338. spin_unlock(&conn->params.peer->lock);
  339. _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
  340. rxrpc_start_call_timer(call);
  341. _leave("");
  342. }
  343. /*
  344. * Queue a call's work processor, getting a ref to pass to the work queue.
  345. */
  346. bool rxrpc_queue_call(struct rxrpc_call *call)
  347. {
  348. const void *here = __builtin_return_address(0);
  349. int n = atomic_fetch_add_unless(&call->usage, 1, 0);
  350. if (n == 0)
  351. return false;
  352. if (rxrpc_queue_work(&call->processor))
  353. trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
  354. else
  355. rxrpc_put_call(call, rxrpc_call_put_noqueue);
  356. return true;
  357. }
  358. /*
  359. * Queue a call's work processor, passing the callers ref to the work queue.
  360. */
  361. bool __rxrpc_queue_call(struct rxrpc_call *call)
  362. {
  363. const void *here = __builtin_return_address(0);
  364. int n = atomic_read(&call->usage);
  365. ASSERTCMP(n, >=, 1);
  366. if (rxrpc_queue_work(&call->processor))
  367. trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
  368. else
  369. rxrpc_put_call(call, rxrpc_call_put_noqueue);
  370. return true;
  371. }
  372. /*
  373. * Note the re-emergence of a call.
  374. */
  375. void rxrpc_see_call(struct rxrpc_call *call)
  376. {
  377. const void *here = __builtin_return_address(0);
  378. if (call) {
  379. int n = atomic_read(&call->usage);
  380. trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
  381. }
  382. }
  383. /*
  384. * Note the addition of a ref on a call.
  385. */
  386. void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  387. {
  388. const void *here = __builtin_return_address(0);
  389. int n = atomic_inc_return(&call->usage);
  390. trace_rxrpc_call(call, op, n, here, NULL);
  391. }
  392. /*
  393. * Detach a call from its owning socket.
  394. */
  395. void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
  396. {
  397. const void *here = __builtin_return_address(0);
  398. struct rxrpc_connection *conn = call->conn;
  399. bool put = false;
  400. int i;
  401. _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
  402. trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
  403. here, (const void *)call->flags);
  404. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  405. spin_lock_bh(&call->lock);
  406. if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
  407. BUG();
  408. spin_unlock_bh(&call->lock);
  409. del_timer_sync(&call->timer);
  410. /* Make sure we don't get any more notifications */
  411. write_lock_bh(&rx->recvmsg_lock);
  412. if (!list_empty(&call->recvmsg_link)) {
  413. _debug("unlinking once-pending call %p { e=%lx f=%lx }",
  414. call, call->events, call->flags);
  415. list_del(&call->recvmsg_link);
  416. put = true;
  417. }
  418. /* list_empty() must return false in rxrpc_notify_socket() */
  419. call->recvmsg_link.next = NULL;
  420. call->recvmsg_link.prev = NULL;
  421. write_unlock_bh(&rx->recvmsg_lock);
  422. if (put)
  423. rxrpc_put_call(call, rxrpc_call_put);
  424. write_lock(&rx->call_lock);
  425. if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  426. rb_erase(&call->sock_node, &rx->calls);
  427. memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
  428. rxrpc_put_call(call, rxrpc_call_put_userid);
  429. }
  430. list_del(&call->sock_link);
  431. write_unlock(&rx->call_lock);
  432. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
  433. if (conn)
  434. rxrpc_disconnect_call(call);
  435. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
  436. rxrpc_free_skb(call->rxtx_buffer[i],
  437. (call->tx_phase ? rxrpc_skb_tx_cleaned :
  438. rxrpc_skb_rx_cleaned));
  439. call->rxtx_buffer[i] = NULL;
  440. }
  441. _leave("");
  442. }
  443. /*
  444. * Prepare a kernel service call for retry.
  445. */
  446. int rxrpc_prepare_call_for_retry(struct rxrpc_sock *rx, struct rxrpc_call *call)
  447. {
  448. const void *here = __builtin_return_address(0);
  449. int i;
  450. u8 last = 0;
  451. _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
  452. trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
  453. here, (const void *)call->flags);
  454. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  455. ASSERTCMP(call->completion, !=, RXRPC_CALL_REMOTELY_ABORTED);
  456. ASSERTCMP(call->completion, !=, RXRPC_CALL_LOCALLY_ABORTED);
  457. ASSERT(list_empty(&call->recvmsg_link));
  458. del_timer_sync(&call->timer);
  459. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, call->conn);
  460. if (call->conn)
  461. rxrpc_disconnect_call(call);
  462. if (rxrpc_is_service_call(call) ||
  463. !call->tx_phase ||
  464. call->tx_hard_ack != 0 ||
  465. call->rx_hard_ack != 0 ||
  466. call->rx_top != 0)
  467. return -EINVAL;
  468. call->state = RXRPC_CALL_UNINITIALISED;
  469. call->completion = RXRPC_CALL_SUCCEEDED;
  470. call->call_id = 0;
  471. call->cid = 0;
  472. call->cong_cwnd = 0;
  473. call->cong_extra = 0;
  474. call->cong_ssthresh = 0;
  475. call->cong_mode = 0;
  476. call->cong_dup_acks = 0;
  477. call->cong_cumul_acks = 0;
  478. call->acks_lowest_nak = 0;
  479. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
  480. last |= call->rxtx_annotations[i];
  481. call->rxtx_annotations[i] &= RXRPC_TX_ANNO_LAST;
  482. call->rxtx_annotations[i] |= RXRPC_TX_ANNO_RETRANS;
  483. }
  484. _leave(" = 0");
  485. return 0;
  486. }
  487. /*
  488. * release all the calls associated with a socket
  489. */
  490. void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
  491. {
  492. struct rxrpc_call *call;
  493. _enter("%p", rx);
  494. while (!list_empty(&rx->to_be_accepted)) {
  495. call = list_entry(rx->to_be_accepted.next,
  496. struct rxrpc_call, accept_link);
  497. list_del(&call->accept_link);
  498. rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
  499. rxrpc_put_call(call, rxrpc_call_put);
  500. }
  501. while (!list_empty(&rx->sock_calls)) {
  502. call = list_entry(rx->sock_calls.next,
  503. struct rxrpc_call, sock_link);
  504. rxrpc_get_call(call, rxrpc_call_got);
  505. rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
  506. rxrpc_send_abort_packet(call);
  507. rxrpc_release_call(rx, call);
  508. rxrpc_put_call(call, rxrpc_call_put);
  509. }
  510. _leave("");
  511. }
  512. /*
  513. * release a call
  514. */
  515. void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  516. {
  517. struct rxrpc_net *rxnet = call->rxnet;
  518. const void *here = __builtin_return_address(0);
  519. int n;
  520. ASSERT(call != NULL);
  521. n = atomic_dec_return(&call->usage);
  522. trace_rxrpc_call(call, op, n, here, NULL);
  523. ASSERTCMP(n, >=, 0);
  524. if (n == 0) {
  525. _debug("call %d dead", call->debug_id);
  526. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  527. if (!list_empty(&call->link)) {
  528. write_lock(&rxnet->call_lock);
  529. list_del_init(&call->link);
  530. write_unlock(&rxnet->call_lock);
  531. }
  532. rxrpc_cleanup_call(call);
  533. }
  534. }
  535. /*
  536. * Final call destruction under RCU.
  537. */
  538. static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
  539. {
  540. struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
  541. struct rxrpc_net *rxnet = call->rxnet;
  542. rxrpc_put_peer(call->peer);
  543. kfree(call->rxtx_buffer);
  544. kfree(call->rxtx_annotations);
  545. kmem_cache_free(rxrpc_call_jar, call);
  546. if (atomic_dec_and_test(&rxnet->nr_calls))
  547. wake_up_var(&rxnet->nr_calls);
  548. }
  549. /*
  550. * clean up a call
  551. */
  552. void rxrpc_cleanup_call(struct rxrpc_call *call)
  553. {
  554. int i;
  555. _net("DESTROY CALL %d", call->debug_id);
  556. memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
  557. del_timer_sync(&call->timer);
  558. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  559. ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
  560. ASSERTCMP(call->conn, ==, NULL);
  561. /* Clean up the Rx/Tx buffer */
  562. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
  563. rxrpc_free_skb(call->rxtx_buffer[i],
  564. (call->tx_phase ? rxrpc_skb_tx_cleaned :
  565. rxrpc_skb_rx_cleaned));
  566. rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned);
  567. call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
  568. }
  569. /*
  570. * Make sure that all calls are gone from a network namespace. To reach this
  571. * point, any open UDP sockets in that namespace must have been closed, so any
  572. * outstanding calls cannot be doing I/O.
  573. */
  574. void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
  575. {
  576. struct rxrpc_call *call;
  577. _enter("");
  578. if (list_empty(&rxnet->calls))
  579. return;
  580. write_lock(&rxnet->call_lock);
  581. while (!list_empty(&rxnet->calls)) {
  582. call = list_entry(rxnet->calls.next, struct rxrpc_call, link);
  583. _debug("Zapping call %p", call);
  584. rxrpc_see_call(call);
  585. list_del_init(&call->link);
  586. pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
  587. call, atomic_read(&call->usage),
  588. rxrpc_call_states[call->state],
  589. call->flags, call->events);
  590. write_unlock(&rxnet->call_lock);
  591. cond_resched();
  592. write_lock(&rxnet->call_lock);
  593. }
  594. write_unlock(&rxnet->call_lock);
  595. atomic_dec(&rxnet->nr_calls);
  596. wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
  597. }