conn_object.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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_destroy_connection(struct rcu_head *);
  19. static void rxrpc_connection_timer(struct timer_list *timer)
  20. {
  21. struct rxrpc_connection *conn =
  22. container_of(timer, struct rxrpc_connection, timer);
  23. rxrpc_queue_conn(conn);
  24. }
  25. /*
  26. * allocate a new connection
  27. */
  28. struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
  29. {
  30. struct rxrpc_connection *conn;
  31. _enter("");
  32. conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
  33. if (conn) {
  34. INIT_LIST_HEAD(&conn->cache_link);
  35. timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
  36. INIT_WORK(&conn->processor, &rxrpc_process_connection);
  37. INIT_LIST_HEAD(&conn->proc_link);
  38. INIT_LIST_HEAD(&conn->link);
  39. skb_queue_head_init(&conn->rx_queue);
  40. conn->security = &rxrpc_no_security;
  41. spin_lock_init(&conn->state_lock);
  42. conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
  43. conn->size_align = 4;
  44. conn->idle_timestamp = jiffies;
  45. }
  46. _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
  47. return conn;
  48. }
  49. /*
  50. * Look up a connection in the cache by protocol parameters.
  51. *
  52. * If successful, a pointer to the connection is returned, but no ref is taken.
  53. * NULL is returned if there is no match.
  54. *
  55. * When searching for a service call, if we find a peer but no connection, we
  56. * return that through *_peer in case we need to create a new service call.
  57. *
  58. * The caller must be holding the RCU read lock.
  59. */
  60. struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local,
  61. struct sk_buff *skb,
  62. struct rxrpc_peer **_peer)
  63. {
  64. struct rxrpc_connection *conn;
  65. struct rxrpc_conn_proto k;
  66. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  67. struct sockaddr_rxrpc srx;
  68. struct rxrpc_peer *peer;
  69. _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
  70. if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
  71. goto not_found;
  72. if (srx.transport.family != local->srx.transport.family &&
  73. (srx.transport.family == AF_INET &&
  74. local->srx.transport.family != AF_INET6)) {
  75. pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
  76. srx.transport.family,
  77. local->srx.transport.family);
  78. goto not_found;
  79. }
  80. k.epoch = sp->hdr.epoch;
  81. k.cid = sp->hdr.cid & RXRPC_CIDMASK;
  82. if (rxrpc_to_server(sp)) {
  83. /* We need to look up service connections by the full protocol
  84. * parameter set. We look up the peer first as an intermediate
  85. * step and then the connection from the peer's tree.
  86. */
  87. peer = rxrpc_lookup_peer_rcu(local, &srx);
  88. if (!peer)
  89. goto not_found;
  90. *_peer = peer;
  91. conn = rxrpc_find_service_conn_rcu(peer, skb);
  92. if (!conn || atomic_read(&conn->usage) == 0)
  93. goto not_found;
  94. _leave(" = %p", conn);
  95. return conn;
  96. } else {
  97. /* Look up client connections by connection ID alone as their
  98. * IDs are unique for this machine.
  99. */
  100. conn = idr_find(&rxrpc_client_conn_ids,
  101. sp->hdr.cid >> RXRPC_CIDSHIFT);
  102. if (!conn || atomic_read(&conn->usage) == 0) {
  103. _debug("no conn");
  104. goto not_found;
  105. }
  106. if (conn->proto.epoch != k.epoch ||
  107. conn->params.local != local)
  108. goto not_found;
  109. peer = conn->params.peer;
  110. switch (srx.transport.family) {
  111. case AF_INET:
  112. if (peer->srx.transport.sin.sin_port !=
  113. srx.transport.sin.sin_port ||
  114. peer->srx.transport.sin.sin_addr.s_addr !=
  115. srx.transport.sin.sin_addr.s_addr)
  116. goto not_found;
  117. break;
  118. #ifdef CONFIG_AF_RXRPC_IPV6
  119. case AF_INET6:
  120. if (peer->srx.transport.sin6.sin6_port !=
  121. srx.transport.sin6.sin6_port ||
  122. memcmp(&peer->srx.transport.sin6.sin6_addr,
  123. &srx.transport.sin6.sin6_addr,
  124. sizeof(struct in6_addr)) != 0)
  125. goto not_found;
  126. break;
  127. #endif
  128. default:
  129. BUG();
  130. }
  131. _leave(" = %p", conn);
  132. return conn;
  133. }
  134. not_found:
  135. _leave(" = NULL");
  136. return NULL;
  137. }
  138. /*
  139. * Disconnect a call and clear any channel it occupies when that call
  140. * terminates. The caller must hold the channel_lock and must release the
  141. * call's ref on the connection.
  142. */
  143. void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
  144. struct rxrpc_call *call)
  145. {
  146. struct rxrpc_channel *chan =
  147. &conn->channels[call->cid & RXRPC_CHANNELMASK];
  148. _enter("%d,%x", conn->debug_id, call->cid);
  149. if (rcu_access_pointer(chan->call) == call) {
  150. /* Save the result of the call so that we can repeat it if necessary
  151. * through the channel, whilst disposing of the actual call record.
  152. */
  153. trace_rxrpc_disconnect_call(call);
  154. switch (call->completion) {
  155. case RXRPC_CALL_SUCCEEDED:
  156. chan->last_seq = call->rx_hard_ack;
  157. chan->last_type = RXRPC_PACKET_TYPE_ACK;
  158. break;
  159. case RXRPC_CALL_LOCALLY_ABORTED:
  160. chan->last_abort = call->abort_code;
  161. chan->last_type = RXRPC_PACKET_TYPE_ABORT;
  162. break;
  163. default:
  164. chan->last_abort = RX_USER_ABORT;
  165. chan->last_type = RXRPC_PACKET_TYPE_ABORT;
  166. break;
  167. }
  168. /* Sync with rxrpc_conn_retransmit(). */
  169. smp_wmb();
  170. chan->last_call = chan->call_id;
  171. chan->call_id = chan->call_counter;
  172. rcu_assign_pointer(chan->call, NULL);
  173. }
  174. _leave("");
  175. }
  176. /*
  177. * Disconnect a call and clear any channel it occupies when that call
  178. * terminates.
  179. */
  180. void rxrpc_disconnect_call(struct rxrpc_call *call)
  181. {
  182. struct rxrpc_connection *conn = call->conn;
  183. call->peer->cong_cwnd = call->cong_cwnd;
  184. if (!hlist_unhashed(&call->error_link)) {
  185. spin_lock_bh(&call->peer->lock);
  186. hlist_del_rcu(&call->error_link);
  187. spin_unlock_bh(&call->peer->lock);
  188. }
  189. if (rxrpc_is_client_call(call))
  190. return rxrpc_disconnect_client_call(conn->bundle, call);
  191. spin_lock(&conn->bundle->channel_lock);
  192. __rxrpc_disconnect_call(conn, call);
  193. spin_unlock(&conn->bundle->channel_lock);
  194. set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
  195. conn->idle_timestamp = jiffies;
  196. }
  197. /*
  198. * Kill off a connection.
  199. */
  200. void rxrpc_kill_connection(struct rxrpc_connection *conn)
  201. {
  202. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  203. ASSERT(!rcu_access_pointer(conn->channels[0].call) &&
  204. !rcu_access_pointer(conn->channels[1].call) &&
  205. !rcu_access_pointer(conn->channels[2].call) &&
  206. !rcu_access_pointer(conn->channels[3].call));
  207. ASSERT(list_empty(&conn->cache_link));
  208. write_lock(&rxnet->conn_lock);
  209. list_del_init(&conn->proc_link);
  210. write_unlock(&rxnet->conn_lock);
  211. /* Drain the Rx queue. Note that even though we've unpublished, an
  212. * incoming packet could still be being added to our Rx queue, so we
  213. * will need to drain it again in the RCU cleanup handler.
  214. */
  215. rxrpc_purge_queue(&conn->rx_queue);
  216. /* Leave final destruction to RCU. The connection processor work item
  217. * must carry a ref on the connection to prevent us getting here whilst
  218. * it is queued or running.
  219. */
  220. call_rcu(&conn->rcu, rxrpc_destroy_connection);
  221. }
  222. /*
  223. * Queue a connection's work processor, getting a ref to pass to the work
  224. * queue.
  225. */
  226. bool rxrpc_queue_conn(struct rxrpc_connection *conn)
  227. {
  228. const void *here = __builtin_return_address(0);
  229. int n = atomic_fetch_add_unless(&conn->usage, 1, 0);
  230. if (n == 0)
  231. return false;
  232. if (rxrpc_queue_work(&conn->processor))
  233. trace_rxrpc_conn(conn->debug_id, rxrpc_conn_queued, n + 1, here);
  234. else
  235. rxrpc_put_connection(conn);
  236. return true;
  237. }
  238. /*
  239. * Note the re-emergence of a connection.
  240. */
  241. void rxrpc_see_connection(struct rxrpc_connection *conn)
  242. {
  243. const void *here = __builtin_return_address(0);
  244. if (conn) {
  245. int n = atomic_read(&conn->usage);
  246. trace_rxrpc_conn(conn->debug_id, rxrpc_conn_seen, n, here);
  247. }
  248. }
  249. /*
  250. * Get a ref on a connection.
  251. */
  252. struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn)
  253. {
  254. const void *here = __builtin_return_address(0);
  255. int n = atomic_inc_return(&conn->usage);
  256. trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, n, here);
  257. return conn;
  258. }
  259. /*
  260. * Try to get a ref on a connection.
  261. */
  262. struct rxrpc_connection *
  263. rxrpc_get_connection_maybe(struct rxrpc_connection *conn)
  264. {
  265. const void *here = __builtin_return_address(0);
  266. if (conn) {
  267. int n = atomic_fetch_add_unless(&conn->usage, 1, 0);
  268. if (n > 0)
  269. trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, n + 1, here);
  270. else
  271. conn = NULL;
  272. }
  273. return conn;
  274. }
  275. /*
  276. * Set the service connection reap timer.
  277. */
  278. static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
  279. unsigned long reap_at)
  280. {
  281. if (rxnet->live)
  282. timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
  283. }
  284. /*
  285. * Release a service connection
  286. */
  287. void rxrpc_put_service_conn(struct rxrpc_connection *conn)
  288. {
  289. const void *here = __builtin_return_address(0);
  290. unsigned int debug_id = conn->debug_id;
  291. int n;
  292. n = atomic_dec_return(&conn->usage);
  293. trace_rxrpc_conn(debug_id, rxrpc_conn_put_service, n, here);
  294. ASSERTCMP(n, >=, 0);
  295. if (n == 1)
  296. rxrpc_set_service_reap_timer(conn->params.local->rxnet,
  297. jiffies + rxrpc_connection_expiry);
  298. }
  299. /*
  300. * destroy a virtual connection
  301. */
  302. static void rxrpc_destroy_connection(struct rcu_head *rcu)
  303. {
  304. struct rxrpc_connection *conn =
  305. container_of(rcu, struct rxrpc_connection, rcu);
  306. _enter("{%d,u=%d}", conn->debug_id, atomic_read(&conn->usage));
  307. ASSERTCMP(atomic_read(&conn->usage), ==, 0);
  308. _net("DESTROY CONN %d", conn->debug_id);
  309. del_timer_sync(&conn->timer);
  310. rxrpc_purge_queue(&conn->rx_queue);
  311. conn->security->clear(conn);
  312. key_put(conn->params.key);
  313. key_put(conn->server_key);
  314. rxrpc_put_bundle(conn->bundle);
  315. rxrpc_put_peer(conn->params.peer);
  316. if (atomic_dec_and_test(&conn->params.local->rxnet->nr_conns))
  317. wake_up_var(&conn->params.local->rxnet->nr_conns);
  318. rxrpc_put_local(conn->params.local);
  319. kfree(conn);
  320. _leave("");
  321. }
  322. /*
  323. * reap dead service connections
  324. */
  325. void rxrpc_service_connection_reaper(struct work_struct *work)
  326. {
  327. struct rxrpc_connection *conn, *_p;
  328. struct rxrpc_net *rxnet =
  329. container_of(work, struct rxrpc_net, service_conn_reaper);
  330. unsigned long expire_at, earliest, idle_timestamp, now;
  331. LIST_HEAD(graveyard);
  332. _enter("");
  333. now = jiffies;
  334. earliest = now + MAX_JIFFY_OFFSET;
  335. write_lock(&rxnet->conn_lock);
  336. list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
  337. ASSERTCMP(atomic_read(&conn->usage), >, 0);
  338. if (likely(atomic_read(&conn->usage) > 1))
  339. continue;
  340. if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
  341. continue;
  342. if (rxnet->live && !conn->params.local->dead) {
  343. idle_timestamp = READ_ONCE(conn->idle_timestamp);
  344. expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
  345. if (conn->params.local->service_closed)
  346. expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
  347. _debug("reap CONN %d { u=%d,t=%ld }",
  348. conn->debug_id, atomic_read(&conn->usage),
  349. (long)expire_at - (long)now);
  350. if (time_before(now, expire_at)) {
  351. if (time_before(expire_at, earliest))
  352. earliest = expire_at;
  353. continue;
  354. }
  355. }
  356. /* The usage count sits at 1 whilst the object is unused on the
  357. * list; we reduce that to 0 to make the object unavailable.
  358. */
  359. if (atomic_cmpxchg(&conn->usage, 1, 0) != 1)
  360. continue;
  361. trace_rxrpc_conn(conn->debug_id, rxrpc_conn_reap_service, 0, NULL);
  362. if (rxrpc_conn_is_client(conn))
  363. BUG();
  364. else
  365. rxrpc_unpublish_service_conn(conn);
  366. list_move_tail(&conn->link, &graveyard);
  367. }
  368. write_unlock(&rxnet->conn_lock);
  369. if (earliest != now + MAX_JIFFY_OFFSET) {
  370. _debug("reschedule reaper %ld", (long)earliest - (long)now);
  371. ASSERT(time_after(earliest, now));
  372. rxrpc_set_service_reap_timer(rxnet, earliest);
  373. }
  374. while (!list_empty(&graveyard)) {
  375. conn = list_entry(graveyard.next, struct rxrpc_connection,
  376. link);
  377. list_del_init(&conn->link);
  378. ASSERTCMP(atomic_read(&conn->usage), ==, 0);
  379. rxrpc_kill_connection(conn);
  380. }
  381. _leave("");
  382. }
  383. /*
  384. * preemptively destroy all the service connection records rather than
  385. * waiting for them to time out
  386. */
  387. void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
  388. {
  389. struct rxrpc_connection *conn, *_p;
  390. bool leak = false;
  391. _enter("");
  392. atomic_dec(&rxnet->nr_conns);
  393. rxrpc_destroy_all_client_connections(rxnet);
  394. del_timer_sync(&rxnet->service_conn_reap_timer);
  395. rxrpc_queue_work(&rxnet->service_conn_reaper);
  396. flush_workqueue(rxrpc_workqueue);
  397. write_lock(&rxnet->conn_lock);
  398. list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
  399. pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
  400. conn, atomic_read(&conn->usage));
  401. leak = true;
  402. }
  403. write_unlock(&rxnet->conn_lock);
  404. BUG_ON(leak);
  405. ASSERT(list_empty(&rxnet->conn_proc_list));
  406. /* We need to wait for the connections to be destroyed by RCU as they
  407. * pin things that we still need to get rid of.
  408. */
  409. wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));
  410. _leave("");
  411. }