call_object.c 19 KB

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