call_object.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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_RECV_REQUEST] = "SvRcvReq",
  23. [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq",
  24. [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl",
  25. [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK",
  26. [RXRPC_CALL_COMPLETE] = "Complete",
  27. };
  28. const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
  29. [RXRPC_CALL_SUCCEEDED] = "Complete",
  30. [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort",
  31. [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort",
  32. [RXRPC_CALL_LOCAL_ERROR] = "LocError",
  33. [RXRPC_CALL_NETWORK_ERROR] = "NetError",
  34. };
  35. struct kmem_cache *rxrpc_call_jar;
  36. static DEFINE_SEMAPHORE(rxrpc_call_limiter, 1000);
  37. static DEFINE_SEMAPHORE(rxrpc_kernel_call_limiter, 1000);
  38. void rxrpc_poke_call(struct rxrpc_call *call, enum rxrpc_call_poke_trace what)
  39. {
  40. struct rxrpc_local *local = call->local;
  41. bool busy;
  42. if (!test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) {
  43. spin_lock_bh(&local->lock);
  44. busy = !list_empty(&call->attend_link);
  45. trace_rxrpc_poke_call(call, busy, what);
  46. if (!busy && !rxrpc_try_get_call(call, rxrpc_call_get_poke))
  47. busy = true;
  48. if (!busy) {
  49. list_add_tail(&call->attend_link, &local->call_attend_q);
  50. }
  51. spin_unlock_bh(&local->lock);
  52. if (!busy)
  53. rxrpc_wake_up_io_thread(local);
  54. }
  55. }
  56. static void rxrpc_call_timer_expired(struct timer_list *t)
  57. {
  58. struct rxrpc_call *call = from_timer(call, t, timer);
  59. _enter("%d", call->debug_id);
  60. if (!__rxrpc_call_is_complete(call)) {
  61. trace_rxrpc_timer_expired(call);
  62. rxrpc_poke_call(call, rxrpc_call_poke_timer);
  63. }
  64. }
  65. static struct lock_class_key rxrpc_call_user_mutex_lock_class_key;
  66. static void rxrpc_destroy_call(struct work_struct *);
  67. /*
  68. * find an extant server call
  69. * - called in process context with IRQs enabled
  70. */
  71. struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
  72. unsigned long user_call_ID)
  73. {
  74. struct rxrpc_call *call;
  75. struct rb_node *p;
  76. _enter("%p,%lx", rx, user_call_ID);
  77. read_lock(&rx->call_lock);
  78. p = rx->calls.rb_node;
  79. while (p) {
  80. call = rb_entry(p, struct rxrpc_call, sock_node);
  81. if (user_call_ID < call->user_call_ID)
  82. p = p->rb_left;
  83. else if (user_call_ID > call->user_call_ID)
  84. p = p->rb_right;
  85. else
  86. goto found_extant_call;
  87. }
  88. read_unlock(&rx->call_lock);
  89. _leave(" = NULL");
  90. return NULL;
  91. found_extant_call:
  92. rxrpc_get_call(call, rxrpc_call_get_sendmsg);
  93. read_unlock(&rx->call_lock);
  94. _leave(" = %p [%d]", call, refcount_read(&call->ref));
  95. return call;
  96. }
  97. /*
  98. * allocate a new call
  99. */
  100. struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp,
  101. unsigned int debug_id)
  102. {
  103. struct rxrpc_call *call;
  104. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  105. call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
  106. if (!call)
  107. return NULL;
  108. mutex_init(&call->user_mutex);
  109. /* Prevent lockdep reporting a deadlock false positive between the afs
  110. * filesystem and sys_sendmsg() via the mmap sem.
  111. */
  112. if (rx->sk.sk_kern_sock)
  113. lockdep_set_class(&call->user_mutex,
  114. &rxrpc_call_user_mutex_lock_class_key);
  115. timer_setup(&call->timer, rxrpc_call_timer_expired, 0);
  116. INIT_WORK(&call->destroyer, rxrpc_destroy_call);
  117. INIT_LIST_HEAD(&call->link);
  118. INIT_LIST_HEAD(&call->wait_link);
  119. INIT_LIST_HEAD(&call->accept_link);
  120. INIT_LIST_HEAD(&call->recvmsg_link);
  121. INIT_LIST_HEAD(&call->sock_link);
  122. INIT_LIST_HEAD(&call->attend_link);
  123. INIT_LIST_HEAD(&call->tx_sendmsg);
  124. INIT_LIST_HEAD(&call->tx_buffer);
  125. skb_queue_head_init(&call->recvmsg_queue);
  126. skb_queue_head_init(&call->rx_oos_queue);
  127. init_waitqueue_head(&call->waitq);
  128. spin_lock_init(&call->notify_lock);
  129. spin_lock_init(&call->tx_lock);
  130. refcount_set(&call->ref, 1);
  131. call->debug_id = debug_id;
  132. call->tx_total_len = -1;
  133. call->next_rx_timo = 20 * HZ;
  134. call->next_req_timo = 1 * HZ;
  135. call->ackr_window = 1;
  136. call->ackr_wtop = 1;
  137. call->delay_ack_at = KTIME_MAX;
  138. call->ack_lost_at = KTIME_MAX;
  139. call->resend_at = KTIME_MAX;
  140. call->ping_at = KTIME_MAX;
  141. call->keepalive_at = KTIME_MAX;
  142. call->expect_rx_by = KTIME_MAX;
  143. call->expect_req_by = KTIME_MAX;
  144. call->expect_term_by = KTIME_MAX;
  145. memset(&call->sock_node, 0xed, sizeof(call->sock_node));
  146. call->rx_winsize = rxrpc_rx_window_size;
  147. call->tx_winsize = 16;
  148. call->cong_cwnd = RXRPC_MIN_CWND;
  149. call->cong_ssthresh = RXRPC_TX_MAX_WINDOW;
  150. call->rxnet = rxnet;
  151. call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK;
  152. atomic_inc(&rxnet->nr_calls);
  153. return call;
  154. }
  155. /*
  156. * Allocate a new client call.
  157. */
  158. static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
  159. struct rxrpc_conn_parameters *cp,
  160. struct rxrpc_call_params *p,
  161. gfp_t gfp,
  162. unsigned int debug_id)
  163. {
  164. struct rxrpc_call *call;
  165. ktime_t now;
  166. int ret;
  167. _enter("");
  168. call = rxrpc_alloc_call(rx, gfp, debug_id);
  169. if (!call)
  170. return ERR_PTR(-ENOMEM);
  171. now = ktime_get_real();
  172. call->acks_latest_ts = now;
  173. call->cong_tstamp = now;
  174. call->dest_srx = cp->peer->srx;
  175. call->dest_srx.srx_service = cp->service_id;
  176. call->interruptibility = p->interruptibility;
  177. call->tx_total_len = p->tx_total_len;
  178. call->key = key_get(cp->key);
  179. call->peer = rxrpc_get_peer(cp->peer, rxrpc_peer_get_call);
  180. call->local = rxrpc_get_local(cp->local, rxrpc_local_get_call);
  181. call->security_level = cp->security_level;
  182. if (p->kernel)
  183. __set_bit(RXRPC_CALL_KERNEL, &call->flags);
  184. if (cp->upgrade)
  185. __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
  186. if (cp->exclusive)
  187. __set_bit(RXRPC_CALL_EXCLUSIVE, &call->flags);
  188. if (p->timeouts.normal)
  189. call->next_rx_timo = min(p->timeouts.normal, 1);
  190. if (p->timeouts.idle)
  191. call->next_req_timo = min(p->timeouts.idle, 1);
  192. if (p->timeouts.hard)
  193. call->hard_timo = p->timeouts.hard;
  194. ret = rxrpc_init_client_call_security(call);
  195. if (ret < 0) {
  196. rxrpc_prefail_call(call, RXRPC_CALL_LOCAL_ERROR, ret);
  197. rxrpc_put_call(call, rxrpc_call_put_discard_error);
  198. return ERR_PTR(ret);
  199. }
  200. rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_CONN);
  201. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref),
  202. p->user_call_ID, rxrpc_call_new_client);
  203. _leave(" = %p", call);
  204. return call;
  205. }
  206. /*
  207. * Initiate the call ack/resend/expiry timer.
  208. */
  209. void rxrpc_start_call_timer(struct rxrpc_call *call)
  210. {
  211. if (call->hard_timo) {
  212. ktime_t delay = ms_to_ktime(call->hard_timo * 1000);
  213. call->expect_term_by = ktime_add(ktime_get_real(), delay);
  214. trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_hard);
  215. }
  216. call->timer.expires = jiffies;
  217. }
  218. /*
  219. * Wait for a call slot to become available.
  220. */
  221. static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp)
  222. {
  223. struct semaphore *limiter = &rxrpc_call_limiter;
  224. if (p->kernel)
  225. limiter = &rxrpc_kernel_call_limiter;
  226. if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) {
  227. down(limiter);
  228. return limiter;
  229. }
  230. return down_interruptible(limiter) < 0 ? NULL : limiter;
  231. }
  232. /*
  233. * Release a call slot.
  234. */
  235. static void rxrpc_put_call_slot(struct rxrpc_call *call)
  236. {
  237. struct semaphore *limiter = &rxrpc_call_limiter;
  238. if (test_bit(RXRPC_CALL_KERNEL, &call->flags))
  239. limiter = &rxrpc_kernel_call_limiter;
  240. up(limiter);
  241. }
  242. /*
  243. * Start the process of connecting a call. We obtain a peer and a connection
  244. * bundle, but the actual association of a call with a connection is offloaded
  245. * to the I/O thread to simplify locking.
  246. */
  247. static int rxrpc_connect_call(struct rxrpc_call *call, gfp_t gfp)
  248. {
  249. struct rxrpc_local *local = call->local;
  250. int ret = -ENOMEM;
  251. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  252. ret = rxrpc_look_up_bundle(call, gfp);
  253. if (ret < 0)
  254. goto error;
  255. trace_rxrpc_client(NULL, -1, rxrpc_client_queue_new_call);
  256. rxrpc_get_call(call, rxrpc_call_get_io_thread);
  257. spin_lock(&local->client_call_lock);
  258. list_add_tail(&call->wait_link, &local->new_client_calls);
  259. spin_unlock(&local->client_call_lock);
  260. rxrpc_wake_up_io_thread(local);
  261. return 0;
  262. error:
  263. __set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
  264. return ret;
  265. }
  266. /*
  267. * Set up a call for the given parameters.
  268. * - Called with the socket lock held, which it must release.
  269. * - If it returns a call, the call's lock will need releasing by the caller.
  270. */
  271. struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
  272. struct rxrpc_conn_parameters *cp,
  273. struct rxrpc_call_params *p,
  274. gfp_t gfp,
  275. unsigned int debug_id)
  276. __releases(&rx->sk.sk_lock.slock)
  277. __acquires(&call->user_mutex)
  278. {
  279. struct rxrpc_call *call, *xcall;
  280. struct rxrpc_net *rxnet;
  281. struct semaphore *limiter;
  282. struct rb_node *parent, **pp;
  283. int ret;
  284. _enter("%p,%lx", rx, p->user_call_ID);
  285. if (WARN_ON_ONCE(!cp->peer)) {
  286. release_sock(&rx->sk);
  287. return ERR_PTR(-EIO);
  288. }
  289. limiter = rxrpc_get_call_slot(p, gfp);
  290. if (!limiter) {
  291. release_sock(&rx->sk);
  292. return ERR_PTR(-ERESTARTSYS);
  293. }
  294. call = rxrpc_alloc_client_call(rx, cp, p, gfp, debug_id);
  295. if (IS_ERR(call)) {
  296. release_sock(&rx->sk);
  297. up(limiter);
  298. _leave(" = %ld", PTR_ERR(call));
  299. return call;
  300. }
  301. /* We need to protect a partially set up call against the user as we
  302. * will be acting outside the socket lock.
  303. */
  304. mutex_lock(&call->user_mutex);
  305. /* Publish the call, even though it is incompletely set up as yet */
  306. write_lock(&rx->call_lock);
  307. pp = &rx->calls.rb_node;
  308. parent = NULL;
  309. while (*pp) {
  310. parent = *pp;
  311. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  312. if (p->user_call_ID < xcall->user_call_ID)
  313. pp = &(*pp)->rb_left;
  314. else if (p->user_call_ID > xcall->user_call_ID)
  315. pp = &(*pp)->rb_right;
  316. else
  317. goto error_dup_user_ID;
  318. }
  319. rcu_assign_pointer(call->socket, rx);
  320. call->user_call_ID = p->user_call_ID;
  321. __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  322. rxrpc_get_call(call, rxrpc_call_get_userid);
  323. rb_link_node(&call->sock_node, parent, pp);
  324. rb_insert_color(&call->sock_node, &rx->calls);
  325. list_add(&call->sock_link, &rx->sock_calls);
  326. write_unlock(&rx->call_lock);
  327. rxnet = call->rxnet;
  328. spin_lock(&rxnet->call_lock);
  329. list_add_tail_rcu(&call->link, &rxnet->calls);
  330. spin_unlock(&rxnet->call_lock);
  331. /* From this point on, the call is protected by its own lock. */
  332. release_sock(&rx->sk);
  333. /* Set up or get a connection record and set the protocol parameters,
  334. * including channel number and call ID.
  335. */
  336. ret = rxrpc_connect_call(call, gfp);
  337. if (ret < 0)
  338. goto error_attached_to_socket;
  339. _leave(" = %p [new]", call);
  340. return call;
  341. /* We unexpectedly found the user ID in the list after taking
  342. * the call_lock. This shouldn't happen unless the user races
  343. * with itself and tries to add the same user ID twice at the
  344. * same time in different threads.
  345. */
  346. error_dup_user_ID:
  347. write_unlock(&rx->call_lock);
  348. release_sock(&rx->sk);
  349. rxrpc_prefail_call(call, RXRPC_CALL_LOCAL_ERROR, -EEXIST);
  350. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 0,
  351. rxrpc_call_see_userid_exists);
  352. mutex_unlock(&call->user_mutex);
  353. rxrpc_put_call(call, rxrpc_call_put_userid_exists);
  354. _leave(" = -EEXIST");
  355. return ERR_PTR(-EEXIST);
  356. /* We got an error, but the call is attached to the socket and is in
  357. * need of release. However, we might now race with recvmsg() when it
  358. * completion notifies the socket. Return 0 from sys_sendmsg() and
  359. * leave the error to recvmsg() to deal with.
  360. */
  361. error_attached_to_socket:
  362. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), ret,
  363. rxrpc_call_see_connect_failed);
  364. rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
  365. _leave(" = c=%08x [err]", call->debug_id);
  366. return call;
  367. }
  368. /*
  369. * Set up an incoming call. call->conn points to the connection.
  370. * This is called in BH context and isn't allowed to fail.
  371. */
  372. void rxrpc_incoming_call(struct rxrpc_sock *rx,
  373. struct rxrpc_call *call,
  374. struct sk_buff *skb)
  375. {
  376. struct rxrpc_connection *conn = call->conn;
  377. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  378. u32 chan;
  379. _enter(",%d", call->conn->debug_id);
  380. rcu_assign_pointer(call->socket, rx);
  381. call->call_id = sp->hdr.callNumber;
  382. call->dest_srx.srx_service = sp->hdr.serviceId;
  383. call->cid = sp->hdr.cid;
  384. call->cong_tstamp = skb->tstamp;
  385. __set_bit(RXRPC_CALL_EXPOSED, &call->flags);
  386. rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST);
  387. spin_lock(&conn->state_lock);
  388. switch (conn->state) {
  389. case RXRPC_CONN_SERVICE_UNSECURED:
  390. case RXRPC_CONN_SERVICE_CHALLENGING:
  391. __set_bit(RXRPC_CALL_CONN_CHALLENGING, &call->flags);
  392. break;
  393. case RXRPC_CONN_SERVICE:
  394. break;
  395. case RXRPC_CONN_ABORTED:
  396. rxrpc_set_call_completion(call, conn->completion,
  397. conn->abort_code, conn->error);
  398. break;
  399. default:
  400. BUG();
  401. }
  402. rxrpc_get_call(call, rxrpc_call_get_io_thread);
  403. /* Set the channel for this call. We don't get channel_lock as we're
  404. * only defending against the data_ready handler (which we're called
  405. * from) and the RESPONSE packet parser (which is only really
  406. * interested in call_counter and can cope with a disagreement with the
  407. * call pointer).
  408. */
  409. chan = sp->hdr.cid & RXRPC_CHANNELMASK;
  410. conn->channels[chan].call_counter = call->call_id;
  411. conn->channels[chan].call_id = call->call_id;
  412. conn->channels[chan].call = call;
  413. spin_unlock(&conn->state_lock);
  414. spin_lock(&conn->peer->lock);
  415. hlist_add_head(&call->error_link, &conn->peer->error_targets);
  416. spin_unlock(&conn->peer->lock);
  417. rxrpc_start_call_timer(call);
  418. _leave("");
  419. }
  420. /*
  421. * Note the re-emergence of a call.
  422. */
  423. void rxrpc_see_call(struct rxrpc_call *call, enum rxrpc_call_trace why)
  424. {
  425. if (call) {
  426. int r = refcount_read(&call->ref);
  427. trace_rxrpc_call(call->debug_id, r, 0, why);
  428. }
  429. }
  430. struct rxrpc_call *rxrpc_try_get_call(struct rxrpc_call *call,
  431. enum rxrpc_call_trace why)
  432. {
  433. int r;
  434. if (!call || !__refcount_inc_not_zero(&call->ref, &r))
  435. return NULL;
  436. trace_rxrpc_call(call->debug_id, r + 1, 0, why);
  437. return call;
  438. }
  439. /*
  440. * Note the addition of a ref on a call.
  441. */
  442. void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace why)
  443. {
  444. int r;
  445. __refcount_inc(&call->ref, &r);
  446. trace_rxrpc_call(call->debug_id, r + 1, 0, why);
  447. }
  448. /*
  449. * Clean up the Rx skb ring.
  450. */
  451. static void rxrpc_cleanup_ring(struct rxrpc_call *call)
  452. {
  453. rxrpc_purge_queue(&call->recvmsg_queue);
  454. rxrpc_purge_queue(&call->rx_oos_queue);
  455. }
  456. /*
  457. * Detach a call from its owning socket.
  458. */
  459. void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
  460. {
  461. struct rxrpc_connection *conn = call->conn;
  462. bool put = false, putu = false;
  463. _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref));
  464. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref),
  465. call->flags, rxrpc_call_see_release);
  466. if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
  467. BUG();
  468. rxrpc_put_call_slot(call);
  469. /* Make sure we don't get any more notifications */
  470. spin_lock(&rx->recvmsg_lock);
  471. if (!list_empty(&call->recvmsg_link)) {
  472. _debug("unlinking once-pending call %p { e=%lx f=%lx }",
  473. call, call->events, call->flags);
  474. list_del(&call->recvmsg_link);
  475. put = true;
  476. }
  477. /* list_empty() must return false in rxrpc_notify_socket() */
  478. call->recvmsg_link.next = NULL;
  479. call->recvmsg_link.prev = NULL;
  480. spin_unlock(&rx->recvmsg_lock);
  481. if (put)
  482. rxrpc_put_call(call, rxrpc_call_put_unnotify);
  483. write_lock(&rx->call_lock);
  484. if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  485. rb_erase(&call->sock_node, &rx->calls);
  486. memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
  487. putu = true;
  488. }
  489. list_del(&call->sock_link);
  490. write_unlock(&rx->call_lock);
  491. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
  492. if (putu)
  493. rxrpc_put_call(call, rxrpc_call_put_userid);
  494. _leave("");
  495. }
  496. /*
  497. * release all the calls associated with a socket
  498. */
  499. void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
  500. {
  501. struct rxrpc_call *call;
  502. _enter("%p", rx);
  503. while (!list_empty(&rx->to_be_accepted)) {
  504. call = list_entry(rx->to_be_accepted.next,
  505. struct rxrpc_call, accept_link);
  506. list_del(&call->accept_link);
  507. rxrpc_propose_abort(call, RX_CALL_DEAD, -ECONNRESET,
  508. rxrpc_abort_call_sock_release_tba);
  509. rxrpc_put_call(call, rxrpc_call_put_release_sock_tba);
  510. }
  511. while (!list_empty(&rx->sock_calls)) {
  512. call = list_entry(rx->sock_calls.next,
  513. struct rxrpc_call, sock_link);
  514. rxrpc_get_call(call, rxrpc_call_get_release_sock);
  515. rxrpc_propose_abort(call, RX_CALL_DEAD, -ECONNRESET,
  516. rxrpc_abort_call_sock_release);
  517. rxrpc_release_call(rx, call);
  518. rxrpc_put_call(call, rxrpc_call_put_release_sock);
  519. }
  520. _leave("");
  521. }
  522. /*
  523. * release a call
  524. */
  525. void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace why)
  526. {
  527. struct rxrpc_net *rxnet = call->rxnet;
  528. unsigned int debug_id = call->debug_id;
  529. bool dead;
  530. int r;
  531. ASSERT(call != NULL);
  532. dead = __refcount_dec_and_test(&call->ref, &r);
  533. trace_rxrpc_call(debug_id, r - 1, 0, why);
  534. if (dead) {
  535. ASSERTCMP(__rxrpc_call_state(call), ==, RXRPC_CALL_COMPLETE);
  536. if (!list_empty(&call->link)) {
  537. spin_lock(&rxnet->call_lock);
  538. list_del_init(&call->link);
  539. spin_unlock(&rxnet->call_lock);
  540. }
  541. rxrpc_cleanup_call(call);
  542. }
  543. }
  544. /*
  545. * Free up the call under RCU.
  546. */
  547. static void rxrpc_rcu_free_call(struct rcu_head *rcu)
  548. {
  549. struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
  550. struct rxrpc_net *rxnet = READ_ONCE(call->rxnet);
  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 - but must be done in process context.
  557. */
  558. static void rxrpc_destroy_call(struct work_struct *work)
  559. {
  560. struct rxrpc_call *call = container_of(work, struct rxrpc_call, destroyer);
  561. struct rxrpc_txbuf *txb;
  562. del_timer_sync(&call->timer);
  563. rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
  564. rxrpc_cleanup_ring(call);
  565. while ((txb = list_first_entry_or_null(&call->tx_sendmsg,
  566. struct rxrpc_txbuf, call_link))) {
  567. list_del(&txb->call_link);
  568. rxrpc_put_txbuf(txb, rxrpc_txbuf_put_cleaned);
  569. }
  570. while ((txb = list_first_entry_or_null(&call->tx_buffer,
  571. struct rxrpc_txbuf, call_link))) {
  572. list_del(&txb->call_link);
  573. rxrpc_put_txbuf(txb, rxrpc_txbuf_put_cleaned);
  574. }
  575. rxrpc_put_txbuf(call->tx_pending, rxrpc_txbuf_put_cleaned);
  576. rxrpc_put_connection(call->conn, rxrpc_conn_put_call);
  577. rxrpc_deactivate_bundle(call->bundle);
  578. rxrpc_put_bundle(call->bundle, rxrpc_bundle_put_call);
  579. rxrpc_put_peer(call->peer, rxrpc_peer_put_call);
  580. rxrpc_put_local(call->local, rxrpc_local_put_call);
  581. call_rcu(&call->rcu, rxrpc_rcu_free_call);
  582. }
  583. /*
  584. * clean up a call
  585. */
  586. void rxrpc_cleanup_call(struct rxrpc_call *call)
  587. {
  588. memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
  589. ASSERTCMP(__rxrpc_call_state(call), ==, RXRPC_CALL_COMPLETE);
  590. ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
  591. del_timer(&call->timer);
  592. if (rcu_read_lock_held())
  593. /* Can't use the rxrpc workqueue as we need to cancel/flush
  594. * something that may be running/waiting there.
  595. */
  596. schedule_work(&call->destroyer);
  597. else
  598. rxrpc_destroy_call(&call->destroyer);
  599. }
  600. /*
  601. * Make sure that all calls are gone from a network namespace. To reach this
  602. * point, any open UDP sockets in that namespace must have been closed, so any
  603. * outstanding calls cannot be doing I/O.
  604. */
  605. void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
  606. {
  607. struct rxrpc_call *call;
  608. _enter("");
  609. if (!list_empty(&rxnet->calls)) {
  610. spin_lock(&rxnet->call_lock);
  611. while (!list_empty(&rxnet->calls)) {
  612. call = list_entry(rxnet->calls.next,
  613. struct rxrpc_call, link);
  614. _debug("Zapping call %p", call);
  615. rxrpc_see_call(call, rxrpc_call_see_zap);
  616. list_del_init(&call->link);
  617. pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
  618. call, refcount_read(&call->ref),
  619. rxrpc_call_states[__rxrpc_call_state(call)],
  620. call->flags, call->events);
  621. spin_unlock(&rxnet->call_lock);
  622. cond_resched();
  623. spin_lock(&rxnet->call_lock);
  624. }
  625. spin_unlock(&rxnet->call_lock);
  626. }
  627. atomic_dec(&rxnet->nr_calls);
  628. wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
  629. }