call_object.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. spin_lock_init(&call->input_lock);
  119. rwlock_init(&call->state_lock);
  120. atomic_set(&call->usage, 1);
  121. call->debug_id = debug_id;
  122. call->tx_total_len = -1;
  123. call->next_rx_timo = 20 * HZ;
  124. call->next_req_timo = 1 * HZ;
  125. memset(&call->sock_node, 0xed, sizeof(call->sock_node));
  126. /* Leave space in the ring to handle a maxed-out jumbo packet */
  127. call->rx_winsize = rxrpc_rx_window_size;
  128. call->tx_winsize = 16;
  129. call->rx_expect_next = 1;
  130. call->cong_cwnd = 2;
  131. call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
  132. call->rxnet = rxnet;
  133. atomic_inc(&rxnet->nr_calls);
  134. return call;
  135. nomem_2:
  136. kfree(call->rxtx_buffer);
  137. nomem:
  138. kmem_cache_free(rxrpc_call_jar, call);
  139. return NULL;
  140. }
  141. /*
  142. * Allocate a new client call.
  143. */
  144. static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
  145. struct sockaddr_rxrpc *srx,
  146. gfp_t gfp,
  147. unsigned int debug_id)
  148. {
  149. struct rxrpc_call *call;
  150. ktime_t now;
  151. _enter("");
  152. call = rxrpc_alloc_call(rx, gfp, debug_id);
  153. if (!call)
  154. return ERR_PTR(-ENOMEM);
  155. call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
  156. call->service_id = srx->srx_service;
  157. call->tx_phase = true;
  158. now = ktime_get_real();
  159. call->acks_latest_ts = now;
  160. call->cong_tstamp = now;
  161. _leave(" = %p", call);
  162. return call;
  163. }
  164. /*
  165. * Initiate the call ack/resend/expiry timer.
  166. */
  167. static void rxrpc_start_call_timer(struct rxrpc_call *call)
  168. {
  169. unsigned long now = jiffies;
  170. unsigned long j = now + MAX_JIFFY_OFFSET;
  171. call->ack_at = j;
  172. call->ack_lost_at = j;
  173. call->resend_at = j;
  174. call->ping_at = j;
  175. call->expect_rx_by = j;
  176. call->expect_req_by = j;
  177. call->expect_term_by = j;
  178. call->timer.expires = now;
  179. }
  180. /*
  181. * Set up a call for the given parameters.
  182. * - Called with the socket lock held, which it must release.
  183. * - If it returns a call, the call's lock will need releasing by the caller.
  184. */
  185. struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
  186. struct rxrpc_conn_parameters *cp,
  187. struct sockaddr_rxrpc *srx,
  188. struct rxrpc_call_params *p,
  189. gfp_t gfp,
  190. unsigned int debug_id)
  191. __releases(&rx->sk.sk_lock.slock)
  192. __acquires(&call->user_mutex)
  193. {
  194. struct rxrpc_call *call, *xcall;
  195. struct rxrpc_net *rxnet;
  196. struct rb_node *parent, **pp;
  197. const void *here = __builtin_return_address(0);
  198. int ret;
  199. _enter("%p,%lx", rx, p->user_call_ID);
  200. call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id);
  201. if (IS_ERR(call)) {
  202. release_sock(&rx->sk);
  203. _leave(" = %ld", PTR_ERR(call));
  204. return call;
  205. }
  206. call->tx_total_len = p->tx_total_len;
  207. trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage),
  208. here, (const void *)p->user_call_ID);
  209. /* We need to protect a partially set up call against the user as we
  210. * will be acting outside the socket lock.
  211. */
  212. mutex_lock(&call->user_mutex);
  213. /* Publish the call, even though it is incompletely set up as yet */
  214. write_lock(&rx->call_lock);
  215. pp = &rx->calls.rb_node;
  216. parent = NULL;
  217. while (*pp) {
  218. parent = *pp;
  219. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  220. if (p->user_call_ID < xcall->user_call_ID)
  221. pp = &(*pp)->rb_left;
  222. else if (p->user_call_ID > xcall->user_call_ID)
  223. pp = &(*pp)->rb_right;
  224. else
  225. goto error_dup_user_ID;
  226. }
  227. rcu_assign_pointer(call->socket, rx);
  228. call->user_call_ID = p->user_call_ID;
  229. __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  230. rxrpc_get_call(call, rxrpc_call_got_userid);
  231. rb_link_node(&call->sock_node, parent, pp);
  232. rb_insert_color(&call->sock_node, &rx->calls);
  233. list_add(&call->sock_link, &rx->sock_calls);
  234. write_unlock(&rx->call_lock);
  235. rxnet = call->rxnet;
  236. write_lock(&rxnet->call_lock);
  237. list_add_tail(&call->link, &rxnet->calls);
  238. write_unlock(&rxnet->call_lock);
  239. /* From this point on, the call is protected by its own lock. */
  240. release_sock(&rx->sk);
  241. /* Set up or get a connection record and set the protocol parameters,
  242. * including channel number and call ID.
  243. */
  244. ret = rxrpc_connect_call(rx, call, cp, srx, gfp);
  245. if (ret < 0)
  246. goto error_attached_to_socket;
  247. trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
  248. here, NULL);
  249. rxrpc_start_call_timer(call);
  250. _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
  251. _leave(" = %p [new]", call);
  252. return call;
  253. /* We unexpectedly found the user ID in the list after taking
  254. * the call_lock. This shouldn't happen unless the user races
  255. * with itself and tries to add the same user ID twice at the
  256. * same time in different threads.
  257. */
  258. error_dup_user_ID:
  259. write_unlock(&rx->call_lock);
  260. release_sock(&rx->sk);
  261. __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  262. RX_CALL_DEAD, -EEXIST);
  263. trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
  264. here, ERR_PTR(-EEXIST));
  265. rxrpc_release_call(rx, call);
  266. mutex_unlock(&call->user_mutex);
  267. rxrpc_put_call(call, rxrpc_call_put);
  268. _leave(" = -EEXIST");
  269. return ERR_PTR(-EEXIST);
  270. /* We got an error, but the call is attached to the socket and is in
  271. * need of release. However, we might now race with recvmsg() when
  272. * completing the call queues it. Return 0 from sys_sendmsg() and
  273. * leave the error to recvmsg() to deal with.
  274. */
  275. error_attached_to_socket:
  276. trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
  277. here, ERR_PTR(ret));
  278. set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
  279. __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  280. RX_CALL_DEAD, ret);
  281. _leave(" = c=%08x [err]", call->debug_id);
  282. return call;
  283. }
  284. /*
  285. * Retry a call to a new address. It is expected that the Tx queue of the call
  286. * will contain data previously packaged for an old call.
  287. */
  288. int rxrpc_retry_client_call(struct rxrpc_sock *rx,
  289. struct rxrpc_call *call,
  290. struct rxrpc_conn_parameters *cp,
  291. struct sockaddr_rxrpc *srx,
  292. gfp_t gfp)
  293. {
  294. const void *here = __builtin_return_address(0);
  295. int ret;
  296. /* Set up or get a connection record and set the protocol parameters,
  297. * including channel number and call ID.
  298. */
  299. ret = rxrpc_connect_call(rx, call, cp, srx, gfp);
  300. if (ret < 0)
  301. goto error;
  302. trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
  303. here, NULL);
  304. rxrpc_start_call_timer(call);
  305. _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
  306. if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  307. rxrpc_queue_call(call);
  308. _leave(" = 0");
  309. return 0;
  310. error:
  311. rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  312. RX_CALL_DEAD, ret);
  313. trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
  314. here, ERR_PTR(ret));
  315. _leave(" = %d", ret);
  316. return ret;
  317. }
  318. /*
  319. * Set up an incoming call. call->conn points to the connection.
  320. * This is called in BH context and isn't allowed to fail.
  321. */
  322. void rxrpc_incoming_call(struct rxrpc_sock *rx,
  323. struct rxrpc_call *call,
  324. struct sk_buff *skb)
  325. {
  326. struct rxrpc_connection *conn = call->conn;
  327. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  328. u32 chan;
  329. _enter(",%d", call->conn->debug_id);
  330. rcu_assign_pointer(call->socket, rx);
  331. call->call_id = sp->hdr.callNumber;
  332. call->service_id = sp->hdr.serviceId;
  333. call->cid = sp->hdr.cid;
  334. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  335. if (sp->hdr.securityIndex > 0)
  336. call->state = RXRPC_CALL_SERVER_SECURING;
  337. call->cong_tstamp = skb->tstamp;
  338. /* Set the channel for this call. We don't get channel_lock as we're
  339. * only defending against the data_ready handler (which we're called
  340. * from) and the RESPONSE packet parser (which is only really
  341. * interested in call_counter and can cope with a disagreement with the
  342. * call pointer).
  343. */
  344. chan = sp->hdr.cid & RXRPC_CHANNELMASK;
  345. conn->channels[chan].call_counter = call->call_id;
  346. conn->channels[chan].call_id = call->call_id;
  347. rcu_assign_pointer(conn->channels[chan].call, call);
  348. spin_lock(&conn->params.peer->lock);
  349. hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets);
  350. spin_unlock(&conn->params.peer->lock);
  351. _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
  352. rxrpc_start_call_timer(call);
  353. _leave("");
  354. }
  355. /*
  356. * Queue a call's work processor, getting a ref to pass to the work queue.
  357. */
  358. bool rxrpc_queue_call(struct rxrpc_call *call)
  359. {
  360. const void *here = __builtin_return_address(0);
  361. int n = atomic_fetch_add_unless(&call->usage, 1, 0);
  362. if (n == 0)
  363. return false;
  364. if (rxrpc_queue_work(&call->processor))
  365. trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
  366. else
  367. rxrpc_put_call(call, rxrpc_call_put_noqueue);
  368. return true;
  369. }
  370. /*
  371. * Queue a call's work processor, passing the callers ref to the work queue.
  372. */
  373. bool __rxrpc_queue_call(struct rxrpc_call *call)
  374. {
  375. const void *here = __builtin_return_address(0);
  376. int n = atomic_read(&call->usage);
  377. ASSERTCMP(n, >=, 1);
  378. if (rxrpc_queue_work(&call->processor))
  379. trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
  380. else
  381. rxrpc_put_call(call, rxrpc_call_put_noqueue);
  382. return true;
  383. }
  384. /*
  385. * Note the re-emergence of a call.
  386. */
  387. void rxrpc_see_call(struct rxrpc_call *call)
  388. {
  389. const void *here = __builtin_return_address(0);
  390. if (call) {
  391. int n = atomic_read(&call->usage);
  392. trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
  393. }
  394. }
  395. /*
  396. * Note the addition of a ref on a call.
  397. */
  398. void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  399. {
  400. const void *here = __builtin_return_address(0);
  401. int n = atomic_inc_return(&call->usage);
  402. trace_rxrpc_call(call, op, n, here, NULL);
  403. }
  404. /*
  405. * Detach a call from its owning socket.
  406. */
  407. void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
  408. {
  409. const void *here = __builtin_return_address(0);
  410. struct rxrpc_connection *conn = call->conn;
  411. bool put = false;
  412. int i;
  413. _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
  414. trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
  415. here, (const void *)call->flags);
  416. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  417. spin_lock_bh(&call->lock);
  418. if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
  419. BUG();
  420. spin_unlock_bh(&call->lock);
  421. del_timer_sync(&call->timer);
  422. /* Make sure we don't get any more notifications */
  423. write_lock_bh(&rx->recvmsg_lock);
  424. if (!list_empty(&call->recvmsg_link)) {
  425. _debug("unlinking once-pending call %p { e=%lx f=%lx }",
  426. call, call->events, call->flags);
  427. list_del(&call->recvmsg_link);
  428. put = true;
  429. }
  430. /* list_empty() must return false in rxrpc_notify_socket() */
  431. call->recvmsg_link.next = NULL;
  432. call->recvmsg_link.prev = NULL;
  433. write_unlock_bh(&rx->recvmsg_lock);
  434. if (put)
  435. rxrpc_put_call(call, rxrpc_call_put);
  436. write_lock(&rx->call_lock);
  437. if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  438. rb_erase(&call->sock_node, &rx->calls);
  439. memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
  440. rxrpc_put_call(call, rxrpc_call_put_userid);
  441. }
  442. list_del(&call->sock_link);
  443. write_unlock(&rx->call_lock);
  444. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
  445. if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
  446. rxrpc_disconnect_call(call);
  447. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
  448. rxrpc_free_skb(call->rxtx_buffer[i],
  449. (call->tx_phase ? rxrpc_skb_tx_cleaned :
  450. rxrpc_skb_rx_cleaned));
  451. call->rxtx_buffer[i] = NULL;
  452. }
  453. _leave("");
  454. }
  455. /*
  456. * Prepare a kernel service call for retry.
  457. */
  458. int rxrpc_prepare_call_for_retry(struct rxrpc_sock *rx, struct rxrpc_call *call)
  459. {
  460. const void *here = __builtin_return_address(0);
  461. int i;
  462. u8 last = 0;
  463. _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
  464. trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
  465. here, (const void *)call->flags);
  466. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  467. ASSERTCMP(call->completion, !=, RXRPC_CALL_REMOTELY_ABORTED);
  468. ASSERTCMP(call->completion, !=, RXRPC_CALL_LOCALLY_ABORTED);
  469. ASSERT(list_empty(&call->recvmsg_link));
  470. del_timer_sync(&call->timer);
  471. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, call->conn);
  472. if (call->conn)
  473. rxrpc_disconnect_call(call);
  474. if (rxrpc_is_service_call(call) ||
  475. !call->tx_phase ||
  476. call->tx_hard_ack != 0 ||
  477. call->rx_hard_ack != 0 ||
  478. call->rx_top != 0)
  479. return -EINVAL;
  480. call->state = RXRPC_CALL_UNINITIALISED;
  481. call->completion = RXRPC_CALL_SUCCEEDED;
  482. call->call_id = 0;
  483. call->cid = 0;
  484. call->cong_cwnd = 0;
  485. call->cong_extra = 0;
  486. call->cong_ssthresh = 0;
  487. call->cong_mode = 0;
  488. call->cong_dup_acks = 0;
  489. call->cong_cumul_acks = 0;
  490. call->acks_lowest_nak = 0;
  491. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
  492. last |= call->rxtx_annotations[i];
  493. call->rxtx_annotations[i] &= RXRPC_TX_ANNO_LAST;
  494. call->rxtx_annotations[i] |= RXRPC_TX_ANNO_RETRANS;
  495. }
  496. _leave(" = 0");
  497. return 0;
  498. }
  499. /*
  500. * release all the calls associated with a socket
  501. */
  502. void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
  503. {
  504. struct rxrpc_call *call;
  505. _enter("%p", rx);
  506. while (!list_empty(&rx->to_be_accepted)) {
  507. call = list_entry(rx->to_be_accepted.next,
  508. struct rxrpc_call, accept_link);
  509. list_del(&call->accept_link);
  510. rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
  511. rxrpc_put_call(call, rxrpc_call_put);
  512. }
  513. while (!list_empty(&rx->sock_calls)) {
  514. call = list_entry(rx->sock_calls.next,
  515. struct rxrpc_call, sock_link);
  516. rxrpc_get_call(call, rxrpc_call_got);
  517. rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
  518. rxrpc_send_abort_packet(call);
  519. rxrpc_release_call(rx, call);
  520. rxrpc_put_call(call, rxrpc_call_put);
  521. }
  522. _leave("");
  523. }
  524. /*
  525. * release a call
  526. */
  527. void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  528. {
  529. struct rxrpc_net *rxnet = call->rxnet;
  530. const void *here = __builtin_return_address(0);
  531. int n;
  532. ASSERT(call != NULL);
  533. n = atomic_dec_return(&call->usage);
  534. trace_rxrpc_call(call, op, n, here, NULL);
  535. ASSERTCMP(n, >=, 0);
  536. if (n == 0) {
  537. _debug("call %d dead", call->debug_id);
  538. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  539. if (!list_empty(&call->link)) {
  540. write_lock(&rxnet->call_lock);
  541. list_del_init(&call->link);
  542. write_unlock(&rxnet->call_lock);
  543. }
  544. rxrpc_cleanup_call(call);
  545. }
  546. }
  547. /*
  548. * Final call destruction - but must be done in process context.
  549. */
  550. static void rxrpc_destroy_call(struct work_struct *work)
  551. {
  552. struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor);
  553. struct rxrpc_net *rxnet = call->rxnet;
  554. rxrpc_put_connection(call->conn);
  555. rxrpc_put_peer(call->peer);
  556. kfree(call->rxtx_buffer);
  557. kfree(call->rxtx_annotations);
  558. kmem_cache_free(rxrpc_call_jar, call);
  559. if (atomic_dec_and_test(&rxnet->nr_calls))
  560. wake_up_var(&rxnet->nr_calls);
  561. }
  562. /*
  563. * Final call destruction under RCU.
  564. */
  565. static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
  566. {
  567. struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
  568. if (in_softirq()) {
  569. INIT_WORK(&call->processor, rxrpc_destroy_call);
  570. if (!rxrpc_queue_work(&call->processor))
  571. BUG();
  572. } else {
  573. rxrpc_destroy_call(&call->processor);
  574. }
  575. }
  576. /*
  577. * clean up a call
  578. */
  579. void rxrpc_cleanup_call(struct rxrpc_call *call)
  580. {
  581. int i;
  582. _net("DESTROY CALL %d", call->debug_id);
  583. memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
  584. del_timer_sync(&call->timer);
  585. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  586. ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
  587. /* Clean up the Rx/Tx buffer */
  588. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
  589. rxrpc_free_skb(call->rxtx_buffer[i],
  590. (call->tx_phase ? rxrpc_skb_tx_cleaned :
  591. rxrpc_skb_rx_cleaned));
  592. rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned);
  593. call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
  594. }
  595. /*
  596. * Make sure that all calls are gone from a network namespace. To reach this
  597. * point, any open UDP sockets in that namespace must have been closed, so any
  598. * outstanding calls cannot be doing I/O.
  599. */
  600. void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
  601. {
  602. struct rxrpc_call *call;
  603. _enter("");
  604. if (!list_empty(&rxnet->calls)) {
  605. write_lock(&rxnet->call_lock);
  606. while (!list_empty(&rxnet->calls)) {
  607. call = list_entry(rxnet->calls.next,
  608. struct rxrpc_call, link);
  609. _debug("Zapping call %p", call);
  610. rxrpc_see_call(call);
  611. list_del_init(&call->link);
  612. pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
  613. call, atomic_read(&call->usage),
  614. rxrpc_call_states[call->state],
  615. call->flags, call->events);
  616. write_unlock(&rxnet->call_lock);
  617. cond_resched();
  618. write_lock(&rxnet->call_lock);
  619. }
  620. write_unlock(&rxnet->call_lock);
  621. }
  622. atomic_dec(&rxnet->nr_calls);
  623. wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
  624. }