conn_object.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RxRPC virtual connection handler, common bits.
  3. *
  4. * Copyright (C) 2007, 2016 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/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/net.h>
  11. #include <linux/skbuff.h>
  12. #include "ar-internal.h"
  13. /*
  14. * Time till a connection expires after last use (in seconds).
  15. */
  16. unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
  17. unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
  18. static void rxrpc_clean_up_connection(struct work_struct *work);
  19. static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
  20. unsigned long reap_at);
  21. void rxrpc_poke_conn(struct rxrpc_connection *conn, enum rxrpc_conn_trace why)
  22. {
  23. struct rxrpc_local *local = conn->local;
  24. bool busy;
  25. if (WARN_ON_ONCE(!local))
  26. return;
  27. spin_lock_irq(&local->lock);
  28. busy = !list_empty(&conn->attend_link);
  29. if (!busy) {
  30. rxrpc_get_connection(conn, why);
  31. list_add_tail(&conn->attend_link, &local->conn_attend_q);
  32. }
  33. spin_unlock_irq(&local->lock);
  34. rxrpc_wake_up_io_thread(local);
  35. }
  36. static void rxrpc_connection_timer(struct timer_list *timer)
  37. {
  38. struct rxrpc_connection *conn =
  39. container_of(timer, struct rxrpc_connection, timer);
  40. rxrpc_poke_conn(conn, rxrpc_conn_get_poke_timer);
  41. }
  42. /*
  43. * allocate a new connection
  44. */
  45. struct rxrpc_connection *rxrpc_alloc_connection(struct rxrpc_net *rxnet,
  46. gfp_t gfp)
  47. {
  48. struct rxrpc_connection *conn;
  49. _enter("");
  50. conn = kzalloc_obj(struct rxrpc_connection, gfp);
  51. if (conn) {
  52. INIT_LIST_HEAD(&conn->cache_link);
  53. timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
  54. INIT_WORK(&conn->processor, rxrpc_process_connection);
  55. INIT_WORK(&conn->destructor, rxrpc_clean_up_connection);
  56. INIT_LIST_HEAD(&conn->proc_link);
  57. INIT_LIST_HEAD(&conn->link);
  58. INIT_LIST_HEAD(&conn->attend_link);
  59. mutex_init(&conn->security_lock);
  60. mutex_init(&conn->tx_data_alloc_lock);
  61. skb_queue_head_init(&conn->rx_queue);
  62. conn->rxnet = rxnet;
  63. conn->security = &rxrpc_no_security;
  64. rwlock_init(&conn->security_use_lock);
  65. spin_lock_init(&conn->state_lock);
  66. conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
  67. conn->idle_timestamp = jiffies;
  68. }
  69. _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
  70. return conn;
  71. }
  72. /*
  73. * Look up a connection in the cache by protocol parameters.
  74. *
  75. * If successful, a pointer to the connection is returned, but no ref is taken.
  76. * NULL is returned if there is no match.
  77. *
  78. * When searching for a service call, if we find a peer but no connection, we
  79. * return that through *_peer in case we need to create a new service call.
  80. *
  81. * The caller must be holding the RCU read lock.
  82. */
  83. struct rxrpc_connection *rxrpc_find_client_connection_rcu(struct rxrpc_local *local,
  84. struct sockaddr_rxrpc *srx,
  85. struct sk_buff *skb)
  86. {
  87. struct rxrpc_connection *conn;
  88. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  89. struct rxrpc_peer *peer;
  90. _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
  91. /* Look up client connections by connection ID alone as their
  92. * IDs are unique for this machine.
  93. */
  94. conn = idr_find(&local->conn_ids, sp->hdr.cid >> RXRPC_CIDSHIFT);
  95. if (!conn || refcount_read(&conn->ref) == 0) {
  96. _debug("no conn");
  97. goto not_found;
  98. }
  99. if (conn->proto.epoch != sp->hdr.epoch ||
  100. conn->local != local)
  101. goto not_found;
  102. peer = conn->peer;
  103. switch (srx->transport.family) {
  104. case AF_INET:
  105. if (peer->srx.transport.sin.sin_port !=
  106. srx->transport.sin.sin_port)
  107. goto not_found;
  108. break;
  109. #ifdef CONFIG_AF_RXRPC_IPV6
  110. case AF_INET6:
  111. if (peer->srx.transport.sin6.sin6_port !=
  112. srx->transport.sin6.sin6_port)
  113. goto not_found;
  114. break;
  115. #endif
  116. default:
  117. BUG();
  118. }
  119. _leave(" = %p", conn);
  120. return conn;
  121. not_found:
  122. _leave(" = NULL");
  123. return NULL;
  124. }
  125. /*
  126. * Disconnect a call and clear any channel it occupies when that call
  127. * terminates. The caller must hold the channel_lock and must release the
  128. * call's ref on the connection.
  129. */
  130. void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
  131. struct rxrpc_call *call)
  132. {
  133. struct rxrpc_channel *chan =
  134. &conn->channels[call->cid & RXRPC_CHANNELMASK];
  135. _enter("%d,%x", conn->debug_id, call->cid);
  136. if (chan->call == call) {
  137. /* Save the result of the call so that we can repeat it if necessary
  138. * through the channel, whilst disposing of the actual call record.
  139. */
  140. trace_rxrpc_disconnect_call(call);
  141. switch (call->completion) {
  142. case RXRPC_CALL_SUCCEEDED:
  143. chan->last_seq = call->rx_highest_seq;
  144. chan->last_type = RXRPC_PACKET_TYPE_ACK;
  145. break;
  146. case RXRPC_CALL_LOCALLY_ABORTED:
  147. chan->last_abort = call->abort_code;
  148. chan->last_type = RXRPC_PACKET_TYPE_ABORT;
  149. break;
  150. default:
  151. chan->last_abort = RX_CALL_DEAD;
  152. chan->last_type = RXRPC_PACKET_TYPE_ABORT;
  153. break;
  154. }
  155. chan->last_call = chan->call_id;
  156. chan->call_id = chan->call_counter;
  157. chan->call = NULL;
  158. }
  159. _leave("");
  160. }
  161. /*
  162. * Disconnect a call and clear any channel it occupies when that call
  163. * terminates.
  164. */
  165. void rxrpc_disconnect_call(struct rxrpc_call *call)
  166. {
  167. struct rxrpc_connection *conn = call->conn;
  168. set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
  169. rxrpc_see_call(call, rxrpc_call_see_disconnected);
  170. call->peer->cong_ssthresh = call->cong_ssthresh;
  171. if (!hlist_unhashed(&call->error_link)) {
  172. spin_lock_irq(&call->peer->lock);
  173. hlist_del_init(&call->error_link);
  174. spin_unlock_irq(&call->peer->lock);
  175. }
  176. if (rxrpc_is_client_call(call)) {
  177. rxrpc_disconnect_client_call(call->bundle, call);
  178. } else {
  179. __rxrpc_disconnect_call(conn, call);
  180. conn->idle_timestamp = jiffies;
  181. if (atomic_dec_and_test(&conn->active))
  182. rxrpc_set_service_reap_timer(conn->rxnet,
  183. jiffies + rxrpc_connection_expiry * HZ);
  184. }
  185. rxrpc_put_call(call, rxrpc_call_put_io_thread);
  186. }
  187. /*
  188. * Queue a connection's work processor, getting a ref to pass to the work
  189. * queue.
  190. */
  191. void rxrpc_queue_conn(struct rxrpc_connection *conn, enum rxrpc_conn_trace why)
  192. {
  193. if (atomic_read(&conn->active) >= 0 &&
  194. rxrpc_queue_work(&conn->processor))
  195. rxrpc_see_connection(conn, why);
  196. }
  197. /*
  198. * Note the re-emergence of a connection.
  199. */
  200. void rxrpc_see_connection(struct rxrpc_connection *conn,
  201. enum rxrpc_conn_trace why)
  202. {
  203. if (conn) {
  204. int r = refcount_read(&conn->ref);
  205. trace_rxrpc_conn(conn->debug_id, r, why);
  206. }
  207. }
  208. /*
  209. * Get a ref on a connection.
  210. */
  211. struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn,
  212. enum rxrpc_conn_trace why)
  213. {
  214. int r;
  215. __refcount_inc(&conn->ref, &r);
  216. trace_rxrpc_conn(conn->debug_id, r + 1, why);
  217. return conn;
  218. }
  219. /*
  220. * Try to get a ref on a connection.
  221. */
  222. struct rxrpc_connection *
  223. rxrpc_get_connection_maybe(struct rxrpc_connection *conn,
  224. enum rxrpc_conn_trace why)
  225. {
  226. int r;
  227. if (conn) {
  228. if (__refcount_inc_not_zero(&conn->ref, &r))
  229. trace_rxrpc_conn(conn->debug_id, r + 1, why);
  230. else
  231. conn = NULL;
  232. }
  233. return conn;
  234. }
  235. /*
  236. * Set the service connection reap timer.
  237. */
  238. static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
  239. unsigned long reap_at)
  240. {
  241. if (rxnet->live)
  242. timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
  243. }
  244. /*
  245. * destroy a virtual connection
  246. */
  247. static void rxrpc_rcu_free_connection(struct rcu_head *rcu)
  248. {
  249. struct rxrpc_connection *conn =
  250. container_of(rcu, struct rxrpc_connection, rcu);
  251. struct rxrpc_net *rxnet = conn->rxnet;
  252. _enter("{%d,u=%d}", conn->debug_id, refcount_read(&conn->ref));
  253. trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
  254. rxrpc_conn_free);
  255. kfree(conn);
  256. if (atomic_dec_and_test(&rxnet->nr_conns))
  257. wake_up_var(&rxnet->nr_conns);
  258. }
  259. /*
  260. * Clean up a dead connection.
  261. */
  262. static void rxrpc_clean_up_connection(struct work_struct *work)
  263. {
  264. struct rxrpc_connection *conn =
  265. container_of(work, struct rxrpc_connection, destructor);
  266. struct rxrpc_net *rxnet = conn->rxnet;
  267. ASSERT(!conn->channels[0].call &&
  268. !conn->channels[1].call &&
  269. !conn->channels[2].call &&
  270. !conn->channels[3].call);
  271. ASSERT(list_empty(&conn->cache_link));
  272. timer_delete_sync(&conn->timer);
  273. cancel_work_sync(&conn->processor); /* Processing may restart the timer */
  274. timer_delete_sync(&conn->timer);
  275. write_lock(&rxnet->conn_lock);
  276. list_del_init(&conn->proc_link);
  277. write_unlock(&rxnet->conn_lock);
  278. if (conn->pmtud_probe) {
  279. trace_rxrpc_pmtud_lost(conn, 0);
  280. conn->peer->pmtud_probing = false;
  281. conn->peer->pmtud_pending = true;
  282. }
  283. rxrpc_purge_queue(&conn->rx_queue);
  284. rxrpc_free_skb(conn->tx_response, rxrpc_skb_put_response);
  285. rxrpc_kill_client_conn(conn);
  286. conn->security->clear(conn);
  287. key_put(conn->key);
  288. rxrpc_put_bundle(conn->bundle, rxrpc_bundle_put_conn);
  289. rxrpc_put_peer(conn->peer, rxrpc_peer_put_conn);
  290. rxrpc_put_local(conn->local, rxrpc_local_put_kill_conn);
  291. /* Drain the Rx queue. Note that even though we've unpublished, an
  292. * incoming packet could still be being added to our Rx queue, so we
  293. * will need to drain it again in the RCU cleanup handler.
  294. */
  295. rxrpc_purge_queue(&conn->rx_queue);
  296. page_frag_cache_drain(&conn->tx_data_alloc);
  297. call_rcu(&conn->rcu, rxrpc_rcu_free_connection);
  298. }
  299. /*
  300. * Drop a ref on a connection.
  301. */
  302. void rxrpc_put_connection(struct rxrpc_connection *conn,
  303. enum rxrpc_conn_trace why)
  304. {
  305. unsigned int debug_id;
  306. bool dead;
  307. int r;
  308. if (!conn)
  309. return;
  310. debug_id = conn->debug_id;
  311. dead = __refcount_dec_and_test(&conn->ref, &r);
  312. trace_rxrpc_conn(debug_id, r - 1, why);
  313. if (dead) {
  314. timer_delete(&conn->timer);
  315. cancel_work(&conn->processor);
  316. if (in_softirq() || work_busy(&conn->processor) ||
  317. timer_pending(&conn->timer))
  318. /* Can't use the rxrpc workqueue as we need to cancel/flush
  319. * something that may be running/waiting there.
  320. */
  321. schedule_work(&conn->destructor);
  322. else
  323. rxrpc_clean_up_connection(&conn->destructor);
  324. }
  325. }
  326. /*
  327. * reap dead service connections
  328. */
  329. void rxrpc_service_connection_reaper(struct work_struct *work)
  330. {
  331. struct rxrpc_connection *conn, *_p;
  332. struct rxrpc_net *rxnet =
  333. container_of(work, struct rxrpc_net, service_conn_reaper);
  334. unsigned long expire_at, earliest, idle_timestamp, now;
  335. int active;
  336. LIST_HEAD(graveyard);
  337. _enter("");
  338. now = jiffies;
  339. earliest = now + MAX_JIFFY_OFFSET;
  340. write_lock(&rxnet->conn_lock);
  341. list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
  342. ASSERTCMP(atomic_read(&conn->active), >=, 0);
  343. if (likely(atomic_read(&conn->active) > 0))
  344. continue;
  345. if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
  346. continue;
  347. if (rxnet->live && !conn->local->dead) {
  348. idle_timestamp = READ_ONCE(conn->idle_timestamp);
  349. expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
  350. if (conn->local->service_closed)
  351. expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
  352. _debug("reap CONN %d { a=%d,t=%ld }",
  353. conn->debug_id, atomic_read(&conn->active),
  354. (long)expire_at - (long)now);
  355. if (time_before(now, expire_at)) {
  356. if (time_before(expire_at, earliest))
  357. earliest = expire_at;
  358. continue;
  359. }
  360. }
  361. /* The activity count sits at 0 whilst the conn is unused on
  362. * the list; we reduce that to -1 to make the conn unavailable.
  363. */
  364. active = 0;
  365. if (!atomic_try_cmpxchg(&conn->active, &active, -1))
  366. continue;
  367. rxrpc_see_connection(conn, rxrpc_conn_see_reap_service);
  368. if (rxrpc_conn_is_client(conn))
  369. BUG();
  370. else
  371. rxrpc_unpublish_service_conn(conn);
  372. list_move_tail(&conn->link, &graveyard);
  373. }
  374. write_unlock(&rxnet->conn_lock);
  375. if (earliest != now + MAX_JIFFY_OFFSET) {
  376. _debug("reschedule reaper %ld", (long)earliest - (long)now);
  377. ASSERT(time_after(earliest, now));
  378. rxrpc_set_service_reap_timer(rxnet, earliest);
  379. }
  380. while (!list_empty(&graveyard)) {
  381. conn = list_entry(graveyard.next, struct rxrpc_connection,
  382. link);
  383. list_del_init(&conn->link);
  384. ASSERTCMP(atomic_read(&conn->active), ==, -1);
  385. rxrpc_put_connection(conn, rxrpc_conn_put_service_reaped);
  386. }
  387. _leave("");
  388. }
  389. /*
  390. * preemptively destroy all the service connection records rather than
  391. * waiting for them to time out
  392. */
  393. void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
  394. {
  395. struct rxrpc_connection *conn, *_p;
  396. bool leak = false;
  397. _enter("");
  398. atomic_dec(&rxnet->nr_conns);
  399. timer_delete_sync(&rxnet->service_conn_reap_timer);
  400. rxrpc_queue_work(&rxnet->service_conn_reaper);
  401. flush_workqueue(rxrpc_workqueue);
  402. write_lock(&rxnet->conn_lock);
  403. list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
  404. pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
  405. conn, refcount_read(&conn->ref));
  406. leak = true;
  407. }
  408. write_unlock(&rxnet->conn_lock);
  409. BUG_ON(leak);
  410. ASSERT(list_empty(&rxnet->conn_proc_list));
  411. /* We need to wait for the connections to be destroyed by RCU as they
  412. * pin things that we still need to get rid of.
  413. */
  414. wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));
  415. _leave("");
  416. }