call_object.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 (dhowells@redhat.com)
  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, atomic_read(&call->usage));
  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. atomic_set(&call->usage, 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. return ERR_PTR(-ERESTARTSYS);
  246. call = rxrpc_alloc_client_call(rx, srx, gfp, debug_id);
  247. if (IS_ERR(call)) {
  248. release_sock(&rx->sk);
  249. up(limiter);
  250. _leave(" = %ld", PTR_ERR(call));
  251. return call;
  252. }
  253. call->interruptibility = p->interruptibility;
  254. call->tx_total_len = p->tx_total_len;
  255. trace_rxrpc_call(call->debug_id, rxrpc_call_new_client,
  256. atomic_read(&call->usage),
  257. here, (const void *)p->user_call_ID);
  258. if (p->kernel)
  259. __set_bit(RXRPC_CALL_KERNEL, &call->flags);
  260. /* We need to protect a partially set up call against the user as we
  261. * will be acting outside the socket lock.
  262. */
  263. mutex_lock(&call->user_mutex);
  264. /* Publish the call, even though it is incompletely set up as yet */
  265. write_lock(&rx->call_lock);
  266. pp = &rx->calls.rb_node;
  267. parent = NULL;
  268. while (*pp) {
  269. parent = *pp;
  270. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  271. if (p->user_call_ID < xcall->user_call_ID)
  272. pp = &(*pp)->rb_left;
  273. else if (p->user_call_ID > xcall->user_call_ID)
  274. pp = &(*pp)->rb_right;
  275. else
  276. goto error_dup_user_ID;
  277. }
  278. rcu_assign_pointer(call->socket, rx);
  279. call->user_call_ID = p->user_call_ID;
  280. __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  281. rxrpc_get_call(call, rxrpc_call_got_userid);
  282. rb_link_node(&call->sock_node, parent, pp);
  283. rb_insert_color(&call->sock_node, &rx->calls);
  284. list_add(&call->sock_link, &rx->sock_calls);
  285. write_unlock(&rx->call_lock);
  286. rxnet = call->rxnet;
  287. write_lock(&rxnet->call_lock);
  288. list_add_tail(&call->link, &rxnet->calls);
  289. write_unlock(&rxnet->call_lock);
  290. /* From this point on, the call is protected by its own lock. */
  291. release_sock(&rx->sk);
  292. /* Set up or get a connection record and set the protocol parameters,
  293. * including channel number and call ID.
  294. */
  295. ret = rxrpc_connect_call(rx, call, cp, srx, gfp);
  296. if (ret < 0)
  297. goto error_attached_to_socket;
  298. trace_rxrpc_call(call->debug_id, rxrpc_call_connected,
  299. atomic_read(&call->usage), here, NULL);
  300. rxrpc_start_call_timer(call);
  301. _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
  302. _leave(" = %p [new]", call);
  303. return call;
  304. /* We unexpectedly found the user ID in the list after taking
  305. * the call_lock. This shouldn't happen unless the user races
  306. * with itself and tries to add the same user ID twice at the
  307. * same time in different threads.
  308. */
  309. error_dup_user_ID:
  310. write_unlock(&rx->call_lock);
  311. release_sock(&rx->sk);
  312. __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  313. RX_CALL_DEAD, -EEXIST);
  314. trace_rxrpc_call(call->debug_id, rxrpc_call_error,
  315. atomic_read(&call->usage), here, ERR_PTR(-EEXIST));
  316. rxrpc_release_call(rx, call);
  317. mutex_unlock(&call->user_mutex);
  318. rxrpc_put_call(call, rxrpc_call_put);
  319. _leave(" = -EEXIST");
  320. return ERR_PTR(-EEXIST);
  321. /* We got an error, but the call is attached to the socket and is in
  322. * need of release. However, we might now race with recvmsg() when
  323. * completing the call queues it. Return 0 from sys_sendmsg() and
  324. * leave the error to recvmsg() to deal with.
  325. */
  326. error_attached_to_socket:
  327. trace_rxrpc_call(call->debug_id, rxrpc_call_error,
  328. atomic_read(&call->usage), here, ERR_PTR(ret));
  329. set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
  330. __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
  331. RX_CALL_DEAD, ret);
  332. _leave(" = c=%08x [err]", call->debug_id);
  333. return call;
  334. }
  335. /*
  336. * Set up an incoming call. call->conn points to the connection.
  337. * This is called in BH context and isn't allowed to fail.
  338. */
  339. void rxrpc_incoming_call(struct rxrpc_sock *rx,
  340. struct rxrpc_call *call,
  341. struct sk_buff *skb)
  342. {
  343. struct rxrpc_connection *conn = call->conn;
  344. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  345. u32 chan;
  346. _enter(",%d", call->conn->debug_id);
  347. rcu_assign_pointer(call->socket, rx);
  348. call->call_id = sp->hdr.callNumber;
  349. call->service_id = sp->hdr.serviceId;
  350. call->cid = sp->hdr.cid;
  351. call->state = RXRPC_CALL_SERVER_SECURING;
  352. call->cong_tstamp = skb->tstamp;
  353. /* Set the channel for this call. We don't get channel_lock as we're
  354. * only defending against the data_ready handler (which we're called
  355. * from) and the RESPONSE packet parser (which is only really
  356. * interested in call_counter and can cope with a disagreement with the
  357. * call pointer).
  358. */
  359. chan = sp->hdr.cid & RXRPC_CHANNELMASK;
  360. conn->channels[chan].call_counter = call->call_id;
  361. conn->channels[chan].call_id = call->call_id;
  362. rcu_assign_pointer(conn->channels[chan].call, call);
  363. spin_lock(&conn->params.peer->lock);
  364. hlist_add_head_rcu(&call->error_link, &conn->params.peer->error_targets);
  365. spin_unlock(&conn->params.peer->lock);
  366. _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
  367. rxrpc_start_call_timer(call);
  368. _leave("");
  369. }
  370. /*
  371. * Queue a call's work processor, getting a ref to pass 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_fetch_add_unless(&call->usage, 1, 0);
  377. if (n == 0)
  378. return false;
  379. if (rxrpc_queue_work(&call->processor))
  380. trace_rxrpc_call(call->debug_id, rxrpc_call_queued, n + 1,
  381. here, NULL);
  382. else
  383. rxrpc_put_call(call, rxrpc_call_put_noqueue);
  384. return true;
  385. }
  386. /*
  387. * Queue a call's work processor, passing the callers ref to the work queue.
  388. */
  389. bool __rxrpc_queue_call(struct rxrpc_call *call)
  390. {
  391. const void *here = __builtin_return_address(0);
  392. int n = atomic_read(&call->usage);
  393. ASSERTCMP(n, >=, 1);
  394. if (rxrpc_queue_work(&call->processor))
  395. trace_rxrpc_call(call->debug_id, rxrpc_call_queued_ref, n,
  396. here, NULL);
  397. else
  398. rxrpc_put_call(call, rxrpc_call_put_noqueue);
  399. return true;
  400. }
  401. /*
  402. * Note the re-emergence of a call.
  403. */
  404. void rxrpc_see_call(struct rxrpc_call *call)
  405. {
  406. const void *here = __builtin_return_address(0);
  407. if (call) {
  408. int n = atomic_read(&call->usage);
  409. trace_rxrpc_call(call->debug_id, rxrpc_call_seen, n,
  410. here, NULL);
  411. }
  412. }
  413. bool rxrpc_try_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  414. {
  415. const void *here = __builtin_return_address(0);
  416. int n = atomic_fetch_add_unless(&call->usage, 1, 0);
  417. if (n == 0)
  418. return false;
  419. trace_rxrpc_call(call->debug_id, op, n, here, NULL);
  420. return true;
  421. }
  422. /*
  423. * Note the addition of a ref on a call.
  424. */
  425. void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  426. {
  427. const void *here = __builtin_return_address(0);
  428. int n = atomic_inc_return(&call->usage);
  429. trace_rxrpc_call(call->debug_id, op, n, here, NULL);
  430. }
  431. /*
  432. * Clean up the RxTx skb ring.
  433. */
  434. static void rxrpc_cleanup_ring(struct rxrpc_call *call)
  435. {
  436. int i;
  437. for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
  438. rxrpc_free_skb(call->rxtx_buffer[i], rxrpc_skb_cleaned);
  439. call->rxtx_buffer[i] = NULL;
  440. }
  441. }
  442. /*
  443. * Detach a call from its owning socket.
  444. */
  445. void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
  446. {
  447. const void *here = __builtin_return_address(0);
  448. struct rxrpc_connection *conn = call->conn;
  449. bool put = false;
  450. _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
  451. trace_rxrpc_call(call->debug_id, rxrpc_call_release,
  452. atomic_read(&call->usage),
  453. here, (const void *)call->flags);
  454. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  455. spin_lock_bh(&call->lock);
  456. if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
  457. BUG();
  458. spin_unlock_bh(&call->lock);
  459. rxrpc_put_call_slot(call);
  460. rxrpc_delete_call_timer(call);
  461. /* Make sure we don't get any more notifications */
  462. write_lock_bh(&rx->recvmsg_lock);
  463. if (!list_empty(&call->recvmsg_link)) {
  464. _debug("unlinking once-pending call %p { e=%lx f=%lx }",
  465. call, call->events, call->flags);
  466. list_del(&call->recvmsg_link);
  467. put = true;
  468. }
  469. /* list_empty() must return false in rxrpc_notify_socket() */
  470. call->recvmsg_link.next = NULL;
  471. call->recvmsg_link.prev = NULL;
  472. write_unlock_bh(&rx->recvmsg_lock);
  473. if (put)
  474. rxrpc_put_call(call, rxrpc_call_put);
  475. write_lock(&rx->call_lock);
  476. if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  477. rb_erase(&call->sock_node, &rx->calls);
  478. memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
  479. rxrpc_put_call(call, rxrpc_call_put_userid);
  480. }
  481. list_del(&call->sock_link);
  482. write_unlock(&rx->call_lock);
  483. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
  484. if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
  485. rxrpc_disconnect_call(call);
  486. if (call->security)
  487. call->security->free_call_crypto(call);
  488. _leave("");
  489. }
  490. /*
  491. * release all the calls associated with a socket
  492. */
  493. void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
  494. {
  495. struct rxrpc_call *call;
  496. _enter("%p", rx);
  497. while (!list_empty(&rx->to_be_accepted)) {
  498. call = list_entry(rx->to_be_accepted.next,
  499. struct rxrpc_call, accept_link);
  500. list_del(&call->accept_link);
  501. rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
  502. rxrpc_put_call(call, rxrpc_call_put);
  503. }
  504. while (!list_empty(&rx->sock_calls)) {
  505. call = list_entry(rx->sock_calls.next,
  506. struct rxrpc_call, sock_link);
  507. rxrpc_get_call(call, rxrpc_call_got);
  508. rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
  509. rxrpc_send_abort_packet(call);
  510. rxrpc_release_call(rx, call);
  511. rxrpc_put_call(call, rxrpc_call_put);
  512. }
  513. _leave("");
  514. }
  515. /*
  516. * release a call
  517. */
  518. void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
  519. {
  520. struct rxrpc_net *rxnet = call->rxnet;
  521. const void *here = __builtin_return_address(0);
  522. unsigned int debug_id = call->debug_id;
  523. int n;
  524. ASSERT(call != NULL);
  525. n = atomic_dec_return(&call->usage);
  526. trace_rxrpc_call(debug_id, op, n, here, NULL);
  527. ASSERTCMP(n, >=, 0);
  528. if (n == 0) {
  529. _debug("call %d dead", call->debug_id);
  530. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  531. if (!list_empty(&call->link)) {
  532. write_lock(&rxnet->call_lock);
  533. list_del_init(&call->link);
  534. write_unlock(&rxnet->call_lock);
  535. }
  536. rxrpc_cleanup_call(call);
  537. }
  538. }
  539. /*
  540. * Final call destruction - but must be done in process context.
  541. */
  542. static void rxrpc_destroy_call(struct work_struct *work)
  543. {
  544. struct rxrpc_call *call = container_of(work, struct rxrpc_call, processor);
  545. struct rxrpc_net *rxnet = call->rxnet;
  546. rxrpc_delete_call_timer(call);
  547. rxrpc_put_connection(call->conn);
  548. rxrpc_put_peer(call->peer);
  549. kfree(call->rxtx_buffer);
  550. kfree(call->rxtx_annotations);
  551. kmem_cache_free(rxrpc_call_jar, call);
  552. if (atomic_dec_and_test(&rxnet->nr_calls))
  553. wake_up_var(&rxnet->nr_calls);
  554. }
  555. /*
  556. * Final call destruction under RCU.
  557. */
  558. static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
  559. {
  560. struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
  561. if (in_softirq()) {
  562. INIT_WORK(&call->processor, rxrpc_destroy_call);
  563. if (!rxrpc_queue_work(&call->processor))
  564. BUG();
  565. } else {
  566. rxrpc_destroy_call(&call->processor);
  567. }
  568. }
  569. /*
  570. * clean up a call
  571. */
  572. void rxrpc_cleanup_call(struct rxrpc_call *call)
  573. {
  574. _net("DESTROY CALL %d", call->debug_id);
  575. memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
  576. ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
  577. ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
  578. rxrpc_cleanup_ring(call);
  579. rxrpc_free_skb(call->tx_pending, rxrpc_skb_cleaned);
  580. call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
  581. }
  582. /*
  583. * Make sure that all calls are gone from a network namespace. To reach this
  584. * point, any open UDP sockets in that namespace must have been closed, so any
  585. * outstanding calls cannot be doing I/O.
  586. */
  587. void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
  588. {
  589. struct rxrpc_call *call;
  590. _enter("");
  591. if (!list_empty(&rxnet->calls)) {
  592. write_lock(&rxnet->call_lock);
  593. while (!list_empty(&rxnet->calls)) {
  594. call = list_entry(rxnet->calls.next,
  595. struct rxrpc_call, link);
  596. _debug("Zapping call %p", call);
  597. rxrpc_see_call(call);
  598. list_del_init(&call->link);
  599. pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
  600. call, atomic_read(&call->usage),
  601. rxrpc_call_states[call->state],
  602. call->flags, call->events);
  603. write_unlock(&rxnet->call_lock);
  604. cond_resched();
  605. write_lock(&rxnet->call_lock);
  606. }
  607. write_unlock(&rxnet->call_lock);
  608. }
  609. atomic_dec(&rxnet->nr_calls);
  610. wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
  611. }