conn_object.c 13 KB

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