call_object.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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_irq(&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_irq(&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 = timer_container_of(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. skb_queue_head_init(&call->recvmsg_queue);
  124. skb_queue_head_init(&call->rx_queue);
  125. skb_queue_head_init(&call->rx_oos_queue);
  126. init_waitqueue_head(&call->waitq);
  127. spin_lock_init(&call->notify_lock);
  128. refcount_set(&call->ref, 1);
  129. call->debug_id = debug_id;
  130. call->tx_total_len = -1;
  131. call->tx_jumbo_max = 1;
  132. call->next_rx_timo = 20 * HZ;
  133. call->next_req_timo = 1 * HZ;
  134. call->ackr_window = 1;
  135. call->ackr_wtop = 1;
  136. call->delay_ack_at = KTIME_MAX;
  137. call->rack_timo_at = KTIME_MAX;
  138. call->ping_at = KTIME_MAX;
  139. call->keepalive_at = KTIME_MAX;
  140. call->expect_rx_by = KTIME_MAX;
  141. call->expect_req_by = KTIME_MAX;
  142. call->expect_term_by = KTIME_MAX;
  143. memset(&call->sock_node, 0xed, sizeof(call->sock_node));
  144. call->rx_winsize = rxrpc_rx_window_size;
  145. call->tx_winsize = 16;
  146. call->cong_cwnd = RXRPC_MIN_CWND;
  147. call->cong_ssthresh = RXRPC_TX_MAX_WINDOW;
  148. rxrpc_call_init_rtt(call);
  149. call->rxnet = rxnet;
  150. call->rtt_avail = RXRPC_CALL_RTT_AVAIL_MASK;
  151. atomic_inc(&rxnet->nr_calls);
  152. return call;
  153. }
  154. /*
  155. * Allocate a new client call.
  156. */
  157. static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
  158. struct rxrpc_conn_parameters *cp,
  159. struct rxrpc_call_params *p,
  160. gfp_t gfp,
  161. unsigned int debug_id)
  162. {
  163. struct rxrpc_call *call;
  164. ktime_t now;
  165. int ret;
  166. _enter("");
  167. call = rxrpc_alloc_call(rx, gfp, debug_id);
  168. if (!call)
  169. return ERR_PTR(-ENOMEM);
  170. now = ktime_get_real();
  171. call->acks_latest_ts = now;
  172. call->cong_tstamp = now;
  173. call->dest_srx = cp->peer->srx;
  174. call->dest_srx.srx_service = cp->service_id;
  175. call->interruptibility = p->interruptibility;
  176. call->tx_total_len = p->tx_total_len;
  177. call->key = key_get(cp->key);
  178. call->peer = rxrpc_get_peer(cp->peer, rxrpc_peer_get_call);
  179. call->local = rxrpc_get_local(cp->local, rxrpc_local_get_call);
  180. call->security_level = cp->security_level;
  181. if (p->kernel)
  182. __set_bit(RXRPC_CALL_KERNEL, &call->flags);
  183. if (cp->upgrade)
  184. __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
  185. if (cp->exclusive)
  186. __set_bit(RXRPC_CALL_EXCLUSIVE, &call->flags);
  187. if (p->timeouts.normal)
  188. call->next_rx_timo = umin(p->timeouts.normal, 1);
  189. if (p->timeouts.idle)
  190. call->next_req_timo = umin(p->timeouts.idle, 1);
  191. if (p->timeouts.hard)
  192. call->hard_timo = p->timeouts.hard;
  193. ret = rxrpc_init_client_call_security(call);
  194. if (ret < 0) {
  195. rxrpc_prefail_call(call, RXRPC_CALL_LOCAL_ERROR, ret);
  196. rxrpc_put_call(call, rxrpc_call_put_discard_error);
  197. return ERR_PTR(ret);
  198. }
  199. rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_CONN);
  200. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref),
  201. p->user_call_ID, rxrpc_call_new_client);
  202. _leave(" = %p", call);
  203. return call;
  204. }
  205. /*
  206. * Initiate the call ack/resend/expiry timer.
  207. */
  208. void rxrpc_start_call_timer(struct rxrpc_call *call)
  209. {
  210. if (call->hard_timo) {
  211. ktime_t delay = ms_to_ktime(call->hard_timo * 1000);
  212. call->expect_term_by = ktime_add(ktime_get_real(), delay);
  213. trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_hard);
  214. }
  215. call->timer.expires = jiffies;
  216. }
  217. /*
  218. * Wait for a call slot to become available.
  219. */
  220. static struct semaphore *rxrpc_get_call_slot(struct rxrpc_call_params *p, gfp_t gfp)
  221. {
  222. struct semaphore *limiter = &rxrpc_call_limiter;
  223. if (p->kernel)
  224. limiter = &rxrpc_kernel_call_limiter;
  225. if (p->interruptibility == RXRPC_UNINTERRUPTIBLE) {
  226. down(limiter);
  227. return limiter;
  228. }
  229. return down_interruptible(limiter) < 0 ? NULL : limiter;
  230. }
  231. /*
  232. * Release a call slot.
  233. */
  234. static void rxrpc_put_call_slot(struct rxrpc_call *call)
  235. {
  236. struct semaphore *limiter = &rxrpc_call_limiter;
  237. if (test_bit(RXRPC_CALL_KERNEL, &call->flags))
  238. limiter = &rxrpc_kernel_call_limiter;
  239. up(limiter);
  240. }
  241. /*
  242. * Start the process of connecting a call. We obtain a peer and a connection
  243. * bundle, but the actual association of a call with a connection is offloaded
  244. * to the I/O thread to simplify locking.
  245. */
  246. static int rxrpc_connect_call(struct rxrpc_call *call, gfp_t gfp)
  247. {
  248. struct rxrpc_local *local = call->local;
  249. int ret = -ENOMEM;
  250. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  251. ret = rxrpc_look_up_bundle(call, gfp);
  252. if (ret < 0)
  253. goto error;
  254. trace_rxrpc_client(NULL, -1, rxrpc_client_queue_new_call);
  255. rxrpc_get_call(call, rxrpc_call_get_io_thread);
  256. spin_lock_irq(&local->client_call_lock);
  257. list_add_tail(&call->wait_link, &local->new_client_calls);
  258. spin_unlock_irq(&local->client_call_lock);
  259. rxrpc_wake_up_io_thread(local);
  260. return 0;
  261. error:
  262. __set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
  263. return ret;
  264. }
  265. /*
  266. * Set up a call for the given parameters.
  267. * - Called with the socket lock held, which it must release.
  268. * - If it returns a call, the call's lock will need releasing by the caller.
  269. */
  270. struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
  271. struct rxrpc_conn_parameters *cp,
  272. struct rxrpc_call_params *p,
  273. gfp_t gfp,
  274. unsigned int debug_id)
  275. __releases(&rx->sk.sk_lock)
  276. __acquires(&call->user_mutex)
  277. {
  278. struct rxrpc_call *call, *xcall;
  279. struct rxrpc_net *rxnet;
  280. struct semaphore *limiter;
  281. struct rb_node *parent, **pp;
  282. int ret;
  283. _enter("%p,%lx", rx, p->user_call_ID);
  284. if (WARN_ON_ONCE(!cp->peer)) {
  285. release_sock(&rx->sk);
  286. return ERR_PTR(-EIO);
  287. }
  288. limiter = rxrpc_get_call_slot(p, gfp);
  289. if (!limiter) {
  290. release_sock(&rx->sk);
  291. return ERR_PTR(-ERESTARTSYS);
  292. }
  293. call = rxrpc_alloc_client_call(rx, cp, p, gfp, debug_id);
  294. if (IS_ERR(call)) {
  295. release_sock(&rx->sk);
  296. up(limiter);
  297. _leave(" = %ld", PTR_ERR(call));
  298. return call;
  299. }
  300. /* We need to protect a partially set up call against the user as we
  301. * will be acting outside the socket lock.
  302. */
  303. mutex_lock(&call->user_mutex);
  304. /* Publish the call, even though it is incompletely set up as yet */
  305. write_lock(&rx->call_lock);
  306. pp = &rx->calls.rb_node;
  307. parent = NULL;
  308. while (*pp) {
  309. parent = *pp;
  310. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  311. if (p->user_call_ID < xcall->user_call_ID)
  312. pp = &(*pp)->rb_left;
  313. else if (p->user_call_ID > xcall->user_call_ID)
  314. pp = &(*pp)->rb_right;
  315. else
  316. goto error_dup_user_ID;
  317. }
  318. rcu_assign_pointer(call->socket, rx);
  319. call->user_call_ID = p->user_call_ID;
  320. __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  321. rxrpc_get_call(call, rxrpc_call_get_userid);
  322. rb_link_node(&call->sock_node, parent, pp);
  323. rb_insert_color(&call->sock_node, &rx->calls);
  324. list_add(&call->sock_link, &rx->sock_calls);
  325. write_unlock(&rx->call_lock);
  326. rxnet = call->rxnet;
  327. spin_lock(&rxnet->call_lock);
  328. list_add_tail_rcu(&call->link, &rxnet->calls);
  329. spin_unlock(&rxnet->call_lock);
  330. /* From this point on, the call is protected by its own lock. */
  331. release_sock(&rx->sk);
  332. /* Set up or get a connection record and set the protocol parameters,
  333. * including channel number and call ID.
  334. */
  335. ret = rxrpc_connect_call(call, gfp);
  336. if (ret < 0)
  337. goto error_attached_to_socket;
  338. _leave(" = %p [new]", call);
  339. return call;
  340. /* We unexpectedly found the user ID in the list after taking
  341. * the call_lock. This shouldn't happen unless the user races
  342. * with itself and tries to add the same user ID twice at the
  343. * same time in different threads.
  344. */
  345. error_dup_user_ID:
  346. write_unlock(&rx->call_lock);
  347. release_sock(&rx->sk);
  348. rxrpc_prefail_call(call, RXRPC_CALL_LOCAL_ERROR, -EEXIST);
  349. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), 0,
  350. rxrpc_call_see_userid_exists);
  351. mutex_unlock(&call->user_mutex);
  352. rxrpc_put_call(call, rxrpc_call_put_userid_exists);
  353. _leave(" = -EEXIST");
  354. return ERR_PTR(-EEXIST);
  355. /* We got an error, but the call is attached to the socket and is in
  356. * need of release. However, we might now race with recvmsg() when it
  357. * completion notifies the socket. Return 0 from sys_sendmsg() and
  358. * leave the error to recvmsg() to deal with.
  359. */
  360. error_attached_to_socket:
  361. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref), ret,
  362. rxrpc_call_see_connect_failed);
  363. rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
  364. _leave(" = c=%08x [err]", call->debug_id);
  365. return call;
  366. }
  367. /*
  368. * Set up an incoming call. call->conn points to the connection.
  369. * This is called with interrupts disabled and isn't allowed to fail.
  370. */
  371. void rxrpc_incoming_call(struct rxrpc_sock *rx,
  372. struct rxrpc_call *call,
  373. struct sk_buff *skb)
  374. {
  375. struct rxrpc_connection *conn = call->conn;
  376. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  377. u32 chan;
  378. _enter(",%d", call->conn->debug_id);
  379. rcu_assign_pointer(call->socket, rx);
  380. call->call_id = sp->hdr.callNumber;
  381. call->dest_srx.srx_service = sp->hdr.serviceId;
  382. call->cid = sp->hdr.cid;
  383. call->cong_tstamp = skb->tstamp;
  384. __set_bit(RXRPC_CALL_EXPOSED, &call->flags);
  385. rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST);
  386. spin_lock(&conn->state_lock);
  387. switch (conn->state) {
  388. case RXRPC_CONN_SERVICE_UNSECURED:
  389. case RXRPC_CONN_SERVICE_CHALLENGING:
  390. __set_bit(RXRPC_CALL_CONN_CHALLENGING, &call->flags);
  391. break;
  392. case RXRPC_CONN_SERVICE:
  393. break;
  394. case RXRPC_CONN_ABORTED:
  395. rxrpc_set_call_completion(call, conn->completion,
  396. conn->abort_code, conn->error);
  397. break;
  398. default:
  399. BUG();
  400. }
  401. rxrpc_get_call(call, rxrpc_call_get_io_thread);
  402. /* Set the channel for this call. We don't get channel_lock as we're
  403. * only defending against the data_ready handler (which we're called
  404. * from) and the RESPONSE packet parser (which is only really
  405. * interested in call_counter and can cope with a disagreement with the
  406. * call pointer).
  407. */
  408. chan = sp->hdr.cid & RXRPC_CHANNELMASK;
  409. conn->channels[chan].call_counter = call->call_id;
  410. conn->channels[chan].call_id = call->call_id;
  411. conn->channels[chan].call = call;
  412. spin_unlock(&conn->state_lock);
  413. spin_lock(&conn->peer->lock);
  414. hlist_add_head(&call->error_link, &conn->peer->error_targets);
  415. spin_unlock(&conn->peer->lock);
  416. rxrpc_start_call_timer(call);
  417. _leave("");
  418. }
  419. /*
  420. * Note the re-emergence of a call.
  421. */
  422. void rxrpc_see_call(struct rxrpc_call *call, enum rxrpc_call_trace why)
  423. {
  424. if (call) {
  425. int r = refcount_read(&call->ref);
  426. trace_rxrpc_call(call->debug_id, r, 0, why);
  427. }
  428. }
  429. struct rxrpc_call *rxrpc_try_get_call(struct rxrpc_call *call,
  430. enum rxrpc_call_trace why)
  431. {
  432. int r;
  433. if (!call || !__refcount_inc_not_zero(&call->ref, &r))
  434. return NULL;
  435. trace_rxrpc_call(call->debug_id, r + 1, 0, why);
  436. return call;
  437. }
  438. /*
  439. * Note the addition of a ref on a call.
  440. */
  441. void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace why)
  442. {
  443. int r;
  444. __refcount_inc(&call->ref, &r);
  445. trace_rxrpc_call(call->debug_id, r + 1, 0, why);
  446. }
  447. /*
  448. * Clean up the transmission buffers.
  449. */
  450. static void rxrpc_cleanup_tx_buffers(struct rxrpc_call *call)
  451. {
  452. struct rxrpc_txqueue *tq, *next;
  453. for (tq = call->tx_queue; tq; tq = next) {
  454. next = tq->next;
  455. for (int i = 0; i < RXRPC_NR_TXQUEUE; i++)
  456. if (tq->bufs[i])
  457. rxrpc_put_txbuf(tq->bufs[i], rxrpc_txbuf_put_cleaned);
  458. trace_rxrpc_tq(call, tq, 0, rxrpc_tq_cleaned);
  459. kfree(tq);
  460. }
  461. }
  462. /*
  463. * Clean up the receive buffers.
  464. */
  465. static void rxrpc_cleanup_rx_buffers(struct rxrpc_call *call)
  466. {
  467. rxrpc_purge_queue(&call->recvmsg_queue);
  468. rxrpc_purge_queue(&call->rx_queue);
  469. rxrpc_purge_queue(&call->rx_oos_queue);
  470. }
  471. /*
  472. * Detach a call from its owning socket.
  473. */
  474. void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
  475. {
  476. struct rxrpc_connection *conn = call->conn;
  477. bool putu = false;
  478. _enter("{%d,%d}", call->debug_id, refcount_read(&call->ref));
  479. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref),
  480. call->flags, rxrpc_call_see_release);
  481. if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
  482. BUG();
  483. rxrpc_put_call_slot(call);
  484. /* Note that at this point, the call may still be on or may have been
  485. * added back on to the socket receive queue. recvmsg() must discard
  486. * released calls. The CALL_RELEASED flag should prevent further
  487. * notifications.
  488. */
  489. spin_lock_irq(&rx->recvmsg_lock);
  490. spin_unlock_irq(&rx->recvmsg_lock);
  491. write_lock(&rx->call_lock);
  492. if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  493. rb_erase(&call->sock_node, &rx->calls);
  494. memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
  495. putu = true;
  496. }
  497. list_del(&call->sock_link);
  498. write_unlock(&rx->call_lock);
  499. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
  500. if (putu)
  501. rxrpc_put_call(call, rxrpc_call_put_userid);
  502. _leave("");
  503. }
  504. /*
  505. * release all the calls associated with a socket
  506. */
  507. void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
  508. {
  509. struct rxrpc_call *call;
  510. _enter("%p", rx);
  511. while (!list_empty(&rx->to_be_accepted)) {
  512. call = list_entry(rx->to_be_accepted.next,
  513. struct rxrpc_call, accept_link);
  514. list_del(&call->accept_link);
  515. rxrpc_propose_abort(call, RX_CALL_DEAD, -ECONNRESET,
  516. rxrpc_abort_call_sock_release_tba);
  517. rxrpc_put_call(call, rxrpc_call_put_release_sock_tba);
  518. }
  519. while (!list_empty(&rx->sock_calls)) {
  520. call = list_entry(rx->sock_calls.next,
  521. struct rxrpc_call, sock_link);
  522. rxrpc_get_call(call, rxrpc_call_get_release_sock);
  523. rxrpc_propose_abort(call, RX_CALL_DEAD, -ECONNRESET,
  524. rxrpc_abort_call_sock_release);
  525. rxrpc_release_call(rx, call);
  526. rxrpc_put_call(call, rxrpc_call_put_release_sock);
  527. }
  528. while ((call = list_first_entry_or_null(&rx->recvmsg_q,
  529. struct rxrpc_call, recvmsg_link))) {
  530. list_del_init(&call->recvmsg_link);
  531. rxrpc_put_call(call, rxrpc_call_put_release_recvmsg_q);
  532. }
  533. _leave("");
  534. }
  535. /*
  536. * release a call
  537. */
  538. void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace why)
  539. {
  540. struct rxrpc_net *rxnet = call->rxnet;
  541. unsigned int debug_id = call->debug_id;
  542. bool dead;
  543. int r;
  544. ASSERT(call != NULL);
  545. dead = __refcount_dec_and_test(&call->ref, &r);
  546. trace_rxrpc_call(debug_id, r - 1, 0, why);
  547. if (dead) {
  548. ASSERTCMP(__rxrpc_call_state(call), ==, RXRPC_CALL_COMPLETE);
  549. spin_lock(&rxnet->call_lock);
  550. list_del_rcu(&call->link);
  551. spin_unlock(&rxnet->call_lock);
  552. rxrpc_cleanup_call(call);
  553. }
  554. }
  555. /*
  556. * Free up the call under RCU.
  557. */
  558. static void rxrpc_rcu_free_call(struct rcu_head *rcu)
  559. {
  560. struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
  561. struct rxrpc_net *rxnet = READ_ONCE(call->rxnet);
  562. kmem_cache_free(rxrpc_call_jar, call);
  563. if (atomic_dec_and_test(&rxnet->nr_calls))
  564. wake_up_var(&rxnet->nr_calls);
  565. }
  566. /*
  567. * Final call destruction - but must be done in process context.
  568. */
  569. static void rxrpc_destroy_call(struct work_struct *work)
  570. {
  571. struct rxrpc_call *call = container_of(work, struct rxrpc_call, destroyer);
  572. timer_delete_sync(&call->timer);
  573. rxrpc_cleanup_tx_buffers(call);
  574. rxrpc_cleanup_rx_buffers(call);
  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. key_put(call->key);
  582. call_rcu(&call->rcu, rxrpc_rcu_free_call);
  583. }
  584. /*
  585. * clean up a call
  586. */
  587. void rxrpc_cleanup_call(struct rxrpc_call *call)
  588. {
  589. memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
  590. ASSERTCMP(__rxrpc_call_state(call), ==, RXRPC_CALL_COMPLETE);
  591. ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
  592. timer_delete(&call->timer);
  593. if (rcu_read_lock_held())
  594. /* Can't use the rxrpc workqueue as we need to cancel/flush
  595. * something that may be running/waiting there.
  596. */
  597. schedule_work(&call->destroyer);
  598. else
  599. rxrpc_destroy_call(&call->destroyer);
  600. }
  601. /*
  602. * Make sure that all calls are gone from a network namespace. To reach this
  603. * point, any open UDP sockets in that namespace must have been closed, so any
  604. * outstanding calls cannot be doing I/O.
  605. */
  606. void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
  607. {
  608. struct rxrpc_call *call;
  609. _enter("");
  610. if (!list_empty(&rxnet->calls)) {
  611. int shown = 0;
  612. spin_lock(&rxnet->call_lock);
  613. list_for_each_entry(call, &rxnet->calls, link) {
  614. rxrpc_see_call(call, rxrpc_call_see_still_live);
  615. pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
  616. call, refcount_read(&call->ref),
  617. rxrpc_call_states[__rxrpc_call_state(call)],
  618. call->flags, call->events);
  619. if (++shown >= 10)
  620. break;
  621. }
  622. spin_unlock(&rxnet->call_lock);
  623. }
  624. atomic_dec(&rxnet->nr_calls);
  625. wait_var_event(&rxnet->nr_calls, !atomic_read(&rxnet->nr_calls));
  626. }
  627. /**
  628. * rxrpc_kernel_query_call_security - Query call's security parameters
  629. * @call: The call to query
  630. * @_service_id: Where to return the service ID
  631. * @_enctype: Where to return the "encoding type"
  632. *
  633. * This queries the security parameters of a call, setting *@_service_id and
  634. * *@_enctype and returning the security class.
  635. *
  636. * Return: The security class protocol number.
  637. */
  638. u8 rxrpc_kernel_query_call_security(struct rxrpc_call *call,
  639. u16 *_service_id, u32 *_enctype)
  640. {
  641. *_service_id = call->dest_srx.srx_service;
  642. *_enctype = call->security_enctype;
  643. return call->security_ix;
  644. }
  645. EXPORT_SYMBOL(rxrpc_kernel_query_call_security);