conn_client.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /* Client connection-specific management code.
  2. *
  3. * Copyright (C) 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. *
  11. *
  12. * Client connections need to be cached for a little while after they've made a
  13. * call so as to handle retransmitted DATA packets in case the server didn't
  14. * receive the final ACK or terminating ABORT we sent it.
  15. *
  16. * Client connections can be in one of a number of cache states:
  17. *
  18. * (1) INACTIVE - The connection is not held in any list and may not have been
  19. * exposed to the world. If it has been previously exposed, it was
  20. * discarded from the idle list after expiring.
  21. *
  22. * (2) WAITING - The connection is waiting for the number of client conns to
  23. * drop below the maximum capacity. Calls may be in progress upon it from
  24. * when it was active and got culled.
  25. *
  26. * The connection is on the rxrpc_waiting_client_conns list which is kept
  27. * in to-be-granted order. Culled conns with waiters go to the back of
  28. * the queue just like new conns.
  29. *
  30. * (3) ACTIVE - The connection has at least one call in progress upon it, it
  31. * may freely grant available channels to new calls and calls may be
  32. * waiting on it for channels to become available.
  33. *
  34. * The connection is on the rxnet->active_client_conns list which is kept
  35. * in activation order for culling purposes.
  36. *
  37. * rxrpc_nr_active_client_conns is held incremented also.
  38. *
  39. * (4) UPGRADE - As for ACTIVE, but only one call may be in progress and is
  40. * being used to probe for service upgrade.
  41. *
  42. * (5) CULLED - The connection got summarily culled to try and free up
  43. * capacity. Calls currently in progress on the connection are allowed to
  44. * continue, but new calls will have to wait. There can be no waiters in
  45. * this state - the conn would have to go to the WAITING state instead.
  46. *
  47. * (6) IDLE - The connection has no calls in progress upon it and must have
  48. * been exposed to the world (ie. the EXPOSED flag must be set). When it
  49. * expires, the EXPOSED flag is cleared and the connection transitions to
  50. * the INACTIVE state.
  51. *
  52. * The connection is on the rxnet->idle_client_conns list which is kept in
  53. * order of how soon they'll expire.
  54. *
  55. * There are flags of relevance to the cache:
  56. *
  57. * (1) EXPOSED - The connection ID got exposed to the world. If this flag is
  58. * set, an extra ref is added to the connection preventing it from being
  59. * reaped when it has no calls outstanding. This flag is cleared and the
  60. * ref dropped when a conn is discarded from the idle list.
  61. *
  62. * This allows us to move terminal call state retransmission to the
  63. * connection and to discard the call immediately we think it is done
  64. * with. It also give us a chance to reuse the connection.
  65. *
  66. * (2) DONT_REUSE - The connection should be discarded as soon as possible and
  67. * should not be reused. This is set when an exclusive connection is used
  68. * or a call ID counter overflows.
  69. *
  70. * The caching state may only be changed if the cache lock is held.
  71. *
  72. * There are two idle client connection expiry durations. If the total number
  73. * of connections is below the reap threshold, we use the normal duration; if
  74. * it's above, we use the fast duration.
  75. */
  76. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  77. #include <linux/slab.h>
  78. #include <linux/idr.h>
  79. #include <linux/timer.h>
  80. #include <linux/sched/signal.h>
  81. #include "ar-internal.h"
  82. __read_mostly unsigned int rxrpc_max_client_connections = 1000;
  83. __read_mostly unsigned int rxrpc_reap_client_connections = 900;
  84. __read_mostly unsigned int rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
  85. __read_mostly unsigned int rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
  86. /*
  87. * We use machine-unique IDs for our client connections.
  88. */
  89. DEFINE_IDR(rxrpc_client_conn_ids);
  90. static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
  91. static void rxrpc_cull_active_client_conns(struct rxrpc_net *);
  92. /*
  93. * Get a connection ID and epoch for a client connection from the global pool.
  94. * The connection struct pointer is then recorded in the idr radix tree. The
  95. * epoch doesn't change until the client is rebooted (or, at least, unless the
  96. * module is unloaded).
  97. */
  98. static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
  99. gfp_t gfp)
  100. {
  101. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  102. int id;
  103. _enter("");
  104. idr_preload(gfp);
  105. spin_lock(&rxrpc_conn_id_lock);
  106. id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
  107. 1, 0x40000000, GFP_NOWAIT);
  108. if (id < 0)
  109. goto error;
  110. spin_unlock(&rxrpc_conn_id_lock);
  111. idr_preload_end();
  112. conn->proto.epoch = rxnet->epoch;
  113. conn->proto.cid = id << RXRPC_CIDSHIFT;
  114. set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
  115. _leave(" [CID %x]", conn->proto.cid);
  116. return 0;
  117. error:
  118. spin_unlock(&rxrpc_conn_id_lock);
  119. idr_preload_end();
  120. _leave(" = %d", id);
  121. return id;
  122. }
  123. /*
  124. * Release a connection ID for a client connection from the global pool.
  125. */
  126. static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
  127. {
  128. if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
  129. spin_lock(&rxrpc_conn_id_lock);
  130. idr_remove(&rxrpc_client_conn_ids,
  131. conn->proto.cid >> RXRPC_CIDSHIFT);
  132. spin_unlock(&rxrpc_conn_id_lock);
  133. }
  134. }
  135. /*
  136. * Destroy the client connection ID tree.
  137. */
  138. void rxrpc_destroy_client_conn_ids(void)
  139. {
  140. struct rxrpc_connection *conn;
  141. int id;
  142. if (!idr_is_empty(&rxrpc_client_conn_ids)) {
  143. idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
  144. pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
  145. conn, atomic_read(&conn->usage));
  146. }
  147. BUG();
  148. }
  149. idr_destroy(&rxrpc_client_conn_ids);
  150. }
  151. /*
  152. * Allocate a client connection.
  153. */
  154. static struct rxrpc_connection *
  155. rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
  156. {
  157. struct rxrpc_connection *conn;
  158. struct rxrpc_net *rxnet = cp->local->rxnet;
  159. int ret;
  160. _enter("");
  161. conn = rxrpc_alloc_connection(gfp);
  162. if (!conn) {
  163. _leave(" = -ENOMEM");
  164. return ERR_PTR(-ENOMEM);
  165. }
  166. atomic_set(&conn->usage, 1);
  167. if (cp->exclusive)
  168. __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  169. if (cp->upgrade)
  170. __set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  171. conn->params = *cp;
  172. conn->out_clientflag = RXRPC_CLIENT_INITIATED;
  173. conn->state = RXRPC_CONN_CLIENT;
  174. conn->service_id = cp->service_id;
  175. ret = rxrpc_get_client_connection_id(conn, gfp);
  176. if (ret < 0)
  177. goto error_0;
  178. ret = rxrpc_init_client_conn_security(conn);
  179. if (ret < 0)
  180. goto error_1;
  181. ret = conn->security->prime_packet_security(conn);
  182. if (ret < 0)
  183. goto error_2;
  184. write_lock(&rxnet->conn_lock);
  185. list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
  186. write_unlock(&rxnet->conn_lock);
  187. /* We steal the caller's peer ref. */
  188. cp->peer = NULL;
  189. rxrpc_get_local(conn->params.local);
  190. key_get(conn->params.key);
  191. trace_rxrpc_conn(conn, rxrpc_conn_new_client, atomic_read(&conn->usage),
  192. __builtin_return_address(0));
  193. trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
  194. _leave(" = %p", conn);
  195. return conn;
  196. error_2:
  197. conn->security->clear(conn);
  198. error_1:
  199. rxrpc_put_client_connection_id(conn);
  200. error_0:
  201. kfree(conn);
  202. _leave(" = %d", ret);
  203. return ERR_PTR(ret);
  204. }
  205. /*
  206. * Determine if a connection may be reused.
  207. */
  208. static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
  209. {
  210. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  211. int id_cursor, id, distance, limit;
  212. if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
  213. goto dont_reuse;
  214. if (conn->proto.epoch != rxnet->epoch)
  215. goto mark_dont_reuse;
  216. /* The IDR tree gets very expensive on memory if the connection IDs are
  217. * widely scattered throughout the number space, so we shall want to
  218. * kill off connections that, say, have an ID more than about four
  219. * times the maximum number of client conns away from the current
  220. * allocation point to try and keep the IDs concentrated.
  221. */
  222. id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
  223. id = conn->proto.cid >> RXRPC_CIDSHIFT;
  224. distance = id - id_cursor;
  225. if (distance < 0)
  226. distance = -distance;
  227. limit = max(rxrpc_max_client_connections * 4, 1024U);
  228. if (distance > limit)
  229. goto mark_dont_reuse;
  230. return true;
  231. mark_dont_reuse:
  232. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  233. dont_reuse:
  234. return false;
  235. }
  236. /*
  237. * Create or find a client connection to use for a call.
  238. *
  239. * If we return with a connection, the call will be on its waiting list. It's
  240. * left to the caller to assign a channel and wake up the call.
  241. */
  242. static int rxrpc_get_client_conn(struct rxrpc_call *call,
  243. struct rxrpc_conn_parameters *cp,
  244. struct sockaddr_rxrpc *srx,
  245. gfp_t gfp)
  246. {
  247. struct rxrpc_connection *conn, *candidate = NULL;
  248. struct rxrpc_local *local = cp->local;
  249. struct rb_node *p, **pp, *parent;
  250. long diff;
  251. int ret = -ENOMEM;
  252. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  253. cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
  254. if (!cp->peer)
  255. goto error;
  256. call->cong_cwnd = cp->peer->cong_cwnd;
  257. if (call->cong_cwnd >= call->cong_ssthresh)
  258. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  259. else
  260. call->cong_mode = RXRPC_CALL_SLOW_START;
  261. /* If the connection is not meant to be exclusive, search the available
  262. * connections to see if the connection we want to use already exists.
  263. */
  264. if (!cp->exclusive) {
  265. _debug("search 1");
  266. spin_lock(&local->client_conns_lock);
  267. p = local->client_conns.rb_node;
  268. while (p) {
  269. conn = rb_entry(p, struct rxrpc_connection, client_node);
  270. #define cmp(X) ((long)conn->params.X - (long)cp->X)
  271. diff = (cmp(peer) ?:
  272. cmp(key) ?:
  273. cmp(security_level) ?:
  274. cmp(upgrade));
  275. #undef cmp
  276. if (diff < 0) {
  277. p = p->rb_left;
  278. } else if (diff > 0) {
  279. p = p->rb_right;
  280. } else {
  281. if (rxrpc_may_reuse_conn(conn) &&
  282. rxrpc_get_connection_maybe(conn))
  283. goto found_extant_conn;
  284. /* The connection needs replacing. It's better
  285. * to effect that when we have something to
  286. * replace it with so that we don't have to
  287. * rebalance the tree twice.
  288. */
  289. break;
  290. }
  291. }
  292. spin_unlock(&local->client_conns_lock);
  293. }
  294. /* There wasn't a connection yet or we need an exclusive connection.
  295. * We need to create a candidate and then potentially redo the search
  296. * in case we're racing with another thread also trying to connect on a
  297. * shareable connection.
  298. */
  299. _debug("new conn");
  300. candidate = rxrpc_alloc_client_connection(cp, gfp);
  301. if (IS_ERR(candidate)) {
  302. ret = PTR_ERR(candidate);
  303. goto error_peer;
  304. }
  305. /* Add the call to the new connection's waiting list in case we're
  306. * going to have to wait for the connection to come live. It's our
  307. * connection, so we want first dibs on the channel slots. We would
  308. * normally have to take channel_lock but we do this before anyone else
  309. * can see the connection.
  310. */
  311. list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
  312. if (cp->exclusive) {
  313. call->conn = candidate;
  314. call->security_ix = candidate->security_ix;
  315. call->service_id = candidate->service_id;
  316. _leave(" = 0 [exclusive %d]", candidate->debug_id);
  317. return 0;
  318. }
  319. /* Publish the new connection for userspace to find. We need to redo
  320. * the search before doing this lest we race with someone else adding a
  321. * conflicting instance.
  322. */
  323. _debug("search 2");
  324. spin_lock(&local->client_conns_lock);
  325. pp = &local->client_conns.rb_node;
  326. parent = NULL;
  327. while (*pp) {
  328. parent = *pp;
  329. conn = rb_entry(parent, struct rxrpc_connection, client_node);
  330. #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
  331. diff = (cmp(peer) ?:
  332. cmp(key) ?:
  333. cmp(security_level) ?:
  334. cmp(upgrade));
  335. #undef cmp
  336. if (diff < 0) {
  337. pp = &(*pp)->rb_left;
  338. } else if (diff > 0) {
  339. pp = &(*pp)->rb_right;
  340. } else {
  341. if (rxrpc_may_reuse_conn(conn) &&
  342. rxrpc_get_connection_maybe(conn))
  343. goto found_extant_conn;
  344. /* The old connection is from an outdated epoch. */
  345. _debug("replace conn");
  346. clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
  347. rb_replace_node(&conn->client_node,
  348. &candidate->client_node,
  349. &local->client_conns);
  350. trace_rxrpc_client(conn, -1, rxrpc_client_replace);
  351. goto candidate_published;
  352. }
  353. }
  354. _debug("new conn");
  355. rb_link_node(&candidate->client_node, parent, pp);
  356. rb_insert_color(&candidate->client_node, &local->client_conns);
  357. candidate_published:
  358. set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
  359. call->conn = candidate;
  360. call->security_ix = candidate->security_ix;
  361. call->service_id = candidate->service_id;
  362. spin_unlock(&local->client_conns_lock);
  363. _leave(" = 0 [new %d]", candidate->debug_id);
  364. return 0;
  365. /* We come here if we found a suitable connection already in existence.
  366. * Discard any candidate we may have allocated, and try to get a
  367. * channel on this one.
  368. */
  369. found_extant_conn:
  370. _debug("found conn");
  371. spin_unlock(&local->client_conns_lock);
  372. if (candidate) {
  373. trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
  374. rxrpc_put_connection(candidate);
  375. candidate = NULL;
  376. }
  377. spin_lock(&conn->channel_lock);
  378. call->conn = conn;
  379. call->security_ix = conn->security_ix;
  380. call->service_id = conn->service_id;
  381. list_add(&call->chan_wait_link, &conn->waiting_calls);
  382. spin_unlock(&conn->channel_lock);
  383. _leave(" = 0 [extant %d]", conn->debug_id);
  384. return 0;
  385. error_peer:
  386. rxrpc_put_peer(cp->peer);
  387. cp->peer = NULL;
  388. error:
  389. _leave(" = %d", ret);
  390. return ret;
  391. }
  392. /*
  393. * Activate a connection.
  394. */
  395. static void rxrpc_activate_conn(struct rxrpc_net *rxnet,
  396. struct rxrpc_connection *conn)
  397. {
  398. if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
  399. trace_rxrpc_client(conn, -1, rxrpc_client_to_upgrade);
  400. conn->cache_state = RXRPC_CONN_CLIENT_UPGRADE;
  401. } else {
  402. trace_rxrpc_client(conn, -1, rxrpc_client_to_active);
  403. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  404. }
  405. rxnet->nr_active_client_conns++;
  406. list_move_tail(&conn->cache_link, &rxnet->active_client_conns);
  407. }
  408. /*
  409. * Attempt to animate a connection for a new call.
  410. *
  411. * If it's not exclusive, the connection is in the endpoint tree, and we're in
  412. * the conn's list of those waiting to grab a channel. There is, however, a
  413. * limit on the number of live connections allowed at any one time, so we may
  414. * have to wait for capacity to become available.
  415. *
  416. * Note that a connection on the waiting queue might *also* have active
  417. * channels if it has been culled to make space and then re-requested by a new
  418. * call.
  419. */
  420. static void rxrpc_animate_client_conn(struct rxrpc_net *rxnet,
  421. struct rxrpc_connection *conn)
  422. {
  423. unsigned int nr_conns;
  424. _enter("%d,%d", conn->debug_id, conn->cache_state);
  425. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE ||
  426. conn->cache_state == RXRPC_CONN_CLIENT_UPGRADE)
  427. goto out;
  428. spin_lock(&rxnet->client_conn_cache_lock);
  429. nr_conns = rxnet->nr_client_conns;
  430. if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  431. trace_rxrpc_client(conn, -1, rxrpc_client_count);
  432. rxnet->nr_client_conns = nr_conns + 1;
  433. }
  434. switch (conn->cache_state) {
  435. case RXRPC_CONN_CLIENT_ACTIVE:
  436. case RXRPC_CONN_CLIENT_UPGRADE:
  437. case RXRPC_CONN_CLIENT_WAITING:
  438. break;
  439. case RXRPC_CONN_CLIENT_INACTIVE:
  440. case RXRPC_CONN_CLIENT_CULLED:
  441. case RXRPC_CONN_CLIENT_IDLE:
  442. if (nr_conns >= rxrpc_max_client_connections)
  443. goto wait_for_capacity;
  444. goto activate_conn;
  445. default:
  446. BUG();
  447. }
  448. out_unlock:
  449. spin_unlock(&rxnet->client_conn_cache_lock);
  450. out:
  451. _leave(" [%d]", conn->cache_state);
  452. return;
  453. activate_conn:
  454. _debug("activate");
  455. rxrpc_activate_conn(rxnet, conn);
  456. goto out_unlock;
  457. wait_for_capacity:
  458. _debug("wait");
  459. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  460. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  461. list_move_tail(&conn->cache_link, &rxnet->waiting_client_conns);
  462. goto out_unlock;
  463. }
  464. /*
  465. * Deactivate a channel.
  466. */
  467. static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
  468. unsigned int channel)
  469. {
  470. struct rxrpc_channel *chan = &conn->channels[channel];
  471. rcu_assign_pointer(chan->call, NULL);
  472. conn->active_chans &= ~(1 << channel);
  473. }
  474. /*
  475. * Assign a channel to the call at the front of the queue and wake the call up.
  476. * We don't increment the callNumber counter until this number has been exposed
  477. * to the world.
  478. */
  479. static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
  480. unsigned int channel)
  481. {
  482. struct rxrpc_channel *chan = &conn->channels[channel];
  483. struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
  484. struct rxrpc_call, chan_wait_link);
  485. u32 call_id = chan->call_counter + 1;
  486. trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
  487. write_lock_bh(&call->state_lock);
  488. call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
  489. write_unlock_bh(&call->state_lock);
  490. rxrpc_see_call(call);
  491. list_del_init(&call->chan_wait_link);
  492. conn->active_chans |= 1 << channel;
  493. call->peer = rxrpc_get_peer(conn->params.peer);
  494. call->cid = conn->proto.cid | channel;
  495. call->call_id = call_id;
  496. trace_rxrpc_connect_call(call);
  497. _net("CONNECT call %08x:%08x as call %d on conn %d",
  498. call->cid, call->call_id, call->debug_id, conn->debug_id);
  499. /* Paired with the read barrier in rxrpc_wait_for_channel(). This
  500. * orders cid and epoch in the connection wrt to call_id without the
  501. * need to take the channel_lock.
  502. *
  503. * We provisionally assign a callNumber at this point, but we don't
  504. * confirm it until the call is about to be exposed.
  505. *
  506. * TODO: Pair with a barrier in the data_ready handler when that looks
  507. * at the call ID through a connection channel.
  508. */
  509. smp_wmb();
  510. chan->call_id = call_id;
  511. rcu_assign_pointer(chan->call, call);
  512. wake_up(&call->waitq);
  513. }
  514. /*
  515. * Assign channels and callNumbers to waiting calls with channel_lock
  516. * held by caller.
  517. */
  518. static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
  519. {
  520. u8 avail, mask;
  521. switch (conn->cache_state) {
  522. case RXRPC_CONN_CLIENT_ACTIVE:
  523. mask = RXRPC_ACTIVE_CHANS_MASK;
  524. break;
  525. case RXRPC_CONN_CLIENT_UPGRADE:
  526. mask = 0x01;
  527. break;
  528. default:
  529. return;
  530. }
  531. while (!list_empty(&conn->waiting_calls) &&
  532. (avail = ~conn->active_chans,
  533. avail &= mask,
  534. avail != 0))
  535. rxrpc_activate_one_channel(conn, __ffs(avail));
  536. }
  537. /*
  538. * Assign channels and callNumbers to waiting calls.
  539. */
  540. static void rxrpc_activate_channels(struct rxrpc_connection *conn)
  541. {
  542. _enter("%d", conn->debug_id);
  543. trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
  544. if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
  545. return;
  546. spin_lock(&conn->channel_lock);
  547. rxrpc_activate_channels_locked(conn);
  548. spin_unlock(&conn->channel_lock);
  549. _leave("");
  550. }
  551. /*
  552. * Wait for a callNumber and a channel to be granted to a call.
  553. */
  554. static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
  555. {
  556. int ret = 0;
  557. _enter("%d", call->debug_id);
  558. if (!call->call_id) {
  559. DECLARE_WAITQUEUE(myself, current);
  560. if (!gfpflags_allow_blocking(gfp)) {
  561. ret = -EAGAIN;
  562. goto out;
  563. }
  564. add_wait_queue_exclusive(&call->waitq, &myself);
  565. for (;;) {
  566. set_current_state(TASK_INTERRUPTIBLE);
  567. if (call->call_id)
  568. break;
  569. if (signal_pending(current)) {
  570. ret = -ERESTARTSYS;
  571. break;
  572. }
  573. schedule();
  574. }
  575. remove_wait_queue(&call->waitq, &myself);
  576. __set_current_state(TASK_RUNNING);
  577. }
  578. /* Paired with the write barrier in rxrpc_activate_one_channel(). */
  579. smp_rmb();
  580. out:
  581. _leave(" = %d", ret);
  582. return ret;
  583. }
  584. /*
  585. * find a connection for a call
  586. * - called in process context with IRQs enabled
  587. */
  588. int rxrpc_connect_call(struct rxrpc_call *call,
  589. struct rxrpc_conn_parameters *cp,
  590. struct sockaddr_rxrpc *srx,
  591. gfp_t gfp)
  592. {
  593. struct rxrpc_net *rxnet = cp->local->rxnet;
  594. int ret;
  595. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  596. rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper.work);
  597. rxrpc_cull_active_client_conns(rxnet);
  598. ret = rxrpc_get_client_conn(call, cp, srx, gfp);
  599. if (ret < 0)
  600. return ret;
  601. rxrpc_animate_client_conn(rxnet, call->conn);
  602. rxrpc_activate_channels(call->conn);
  603. ret = rxrpc_wait_for_channel(call, gfp);
  604. if (ret < 0)
  605. rxrpc_disconnect_client_call(call);
  606. _leave(" = %d", ret);
  607. return ret;
  608. }
  609. /*
  610. * Note that a connection is about to be exposed to the world. Once it is
  611. * exposed, we maintain an extra ref on it that stops it from being summarily
  612. * discarded before it's (a) had a chance to deal with retransmission and (b)
  613. * had a chance at re-use (the per-connection security negotiation is
  614. * expensive).
  615. */
  616. static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
  617. unsigned int channel)
  618. {
  619. if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  620. trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
  621. rxrpc_get_connection(conn);
  622. }
  623. }
  624. /*
  625. * Note that a call, and thus a connection, is about to be exposed to the
  626. * world.
  627. */
  628. void rxrpc_expose_client_call(struct rxrpc_call *call)
  629. {
  630. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  631. struct rxrpc_connection *conn = call->conn;
  632. struct rxrpc_channel *chan = &conn->channels[channel];
  633. if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  634. /* Mark the call ID as being used. If the callNumber counter
  635. * exceeds ~2 billion, we kill the connection after its
  636. * outstanding calls have finished so that the counter doesn't
  637. * wrap.
  638. */
  639. chan->call_counter++;
  640. if (chan->call_counter >= INT_MAX)
  641. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  642. rxrpc_expose_client_conn(conn, channel);
  643. }
  644. }
  645. /*
  646. * Disconnect a client call.
  647. */
  648. void rxrpc_disconnect_client_call(struct rxrpc_call *call)
  649. {
  650. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  651. struct rxrpc_connection *conn = call->conn;
  652. struct rxrpc_channel *chan = &conn->channels[channel];
  653. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&call->socket->sk));
  654. trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
  655. call->conn = NULL;
  656. spin_lock(&conn->channel_lock);
  657. /* Calls that have never actually been assigned a channel can simply be
  658. * discarded. If the conn didn't get used either, it will follow
  659. * immediately unless someone else grabs it in the meantime.
  660. */
  661. if (!list_empty(&call->chan_wait_link)) {
  662. _debug("call is waiting");
  663. ASSERTCMP(call->call_id, ==, 0);
  664. ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
  665. list_del_init(&call->chan_wait_link);
  666. trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
  667. /* We must deactivate or idle the connection if it's now
  668. * waiting for nothing.
  669. */
  670. spin_lock(&rxnet->client_conn_cache_lock);
  671. if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
  672. list_empty(&conn->waiting_calls) &&
  673. !conn->active_chans)
  674. goto idle_connection;
  675. goto out;
  676. }
  677. ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
  678. /* If a client call was exposed to the world, we save the result for
  679. * retransmission.
  680. *
  681. * We use a barrier here so that the call number and abort code can be
  682. * read without needing to take a lock.
  683. *
  684. * TODO: Make the incoming packet handler check this and handle
  685. * terminal retransmission without requiring access to the call.
  686. */
  687. if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  688. _debug("exposed %u,%u", call->call_id, call->abort_code);
  689. __rxrpc_disconnect_call(conn, call);
  690. }
  691. /* See if we can pass the channel directly to another call. */
  692. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
  693. !list_empty(&conn->waiting_calls)) {
  694. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  695. rxrpc_activate_one_channel(conn, channel);
  696. goto out_2;
  697. }
  698. /* Things are more complex and we need the cache lock. We might be
  699. * able to simply idle the conn or it might now be lurking on the wait
  700. * list. It might even get moved back to the active list whilst we're
  701. * waiting for the lock.
  702. */
  703. spin_lock(&rxnet->client_conn_cache_lock);
  704. switch (conn->cache_state) {
  705. case RXRPC_CONN_CLIENT_UPGRADE:
  706. /* Deal with termination of a service upgrade probe. */
  707. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  708. clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  709. trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
  710. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  711. rxrpc_activate_channels_locked(conn);
  712. }
  713. /* fall through */
  714. case RXRPC_CONN_CLIENT_ACTIVE:
  715. if (list_empty(&conn->waiting_calls)) {
  716. rxrpc_deactivate_one_channel(conn, channel);
  717. if (!conn->active_chans) {
  718. rxnet->nr_active_client_conns--;
  719. goto idle_connection;
  720. }
  721. goto out;
  722. }
  723. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  724. rxrpc_activate_one_channel(conn, channel);
  725. goto out;
  726. case RXRPC_CONN_CLIENT_CULLED:
  727. rxrpc_deactivate_one_channel(conn, channel);
  728. ASSERT(list_empty(&conn->waiting_calls));
  729. if (!conn->active_chans)
  730. goto idle_connection;
  731. goto out;
  732. case RXRPC_CONN_CLIENT_WAITING:
  733. rxrpc_deactivate_one_channel(conn, channel);
  734. goto out;
  735. default:
  736. BUG();
  737. }
  738. out:
  739. spin_unlock(&rxnet->client_conn_cache_lock);
  740. out_2:
  741. spin_unlock(&conn->channel_lock);
  742. rxrpc_put_connection(conn);
  743. _leave("");
  744. return;
  745. idle_connection:
  746. /* As no channels remain active, the connection gets deactivated
  747. * immediately or moved to the idle list for a short while.
  748. */
  749. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  750. trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
  751. conn->idle_timestamp = jiffies;
  752. conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
  753. list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
  754. if (rxnet->idle_client_conns.next == &conn->cache_link &&
  755. !rxnet->kill_all_client_conns)
  756. queue_delayed_work(rxrpc_workqueue,
  757. &rxnet->client_conn_reaper,
  758. rxrpc_conn_idle_client_expiry);
  759. } else {
  760. trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
  761. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  762. list_del_init(&conn->cache_link);
  763. }
  764. goto out;
  765. }
  766. /*
  767. * Clean up a dead client connection.
  768. */
  769. static struct rxrpc_connection *
  770. rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
  771. {
  772. struct rxrpc_connection *next = NULL;
  773. struct rxrpc_local *local = conn->params.local;
  774. struct rxrpc_net *rxnet = local->rxnet;
  775. unsigned int nr_conns;
  776. trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
  777. if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
  778. spin_lock(&local->client_conns_lock);
  779. if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
  780. &conn->flags))
  781. rb_erase(&conn->client_node, &local->client_conns);
  782. spin_unlock(&local->client_conns_lock);
  783. }
  784. rxrpc_put_client_connection_id(conn);
  785. ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
  786. if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  787. trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
  788. spin_lock(&rxnet->client_conn_cache_lock);
  789. nr_conns = --rxnet->nr_client_conns;
  790. if (nr_conns < rxrpc_max_client_connections &&
  791. !list_empty(&rxnet->waiting_client_conns)) {
  792. next = list_entry(rxnet->waiting_client_conns.next,
  793. struct rxrpc_connection, cache_link);
  794. rxrpc_get_connection(next);
  795. rxrpc_activate_conn(rxnet, next);
  796. }
  797. spin_unlock(&rxnet->client_conn_cache_lock);
  798. }
  799. rxrpc_kill_connection(conn);
  800. if (next)
  801. rxrpc_activate_channels(next);
  802. /* We need to get rid of the temporary ref we took upon next, but we
  803. * can't call rxrpc_put_connection() recursively.
  804. */
  805. return next;
  806. }
  807. /*
  808. * Clean up a dead client connections.
  809. */
  810. void rxrpc_put_client_conn(struct rxrpc_connection *conn)
  811. {
  812. const void *here = __builtin_return_address(0);
  813. int n;
  814. do {
  815. n = atomic_dec_return(&conn->usage);
  816. trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
  817. if (n > 0)
  818. return;
  819. ASSERTCMP(n, >=, 0);
  820. conn = rxrpc_put_one_client_conn(conn);
  821. } while (conn);
  822. }
  823. /*
  824. * Kill the longest-active client connections to make room for new ones.
  825. */
  826. static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet)
  827. {
  828. struct rxrpc_connection *conn;
  829. unsigned int nr_conns = rxnet->nr_client_conns;
  830. unsigned int nr_active, limit;
  831. _enter("");
  832. ASSERTCMP(nr_conns, >=, 0);
  833. if (nr_conns < rxrpc_max_client_connections) {
  834. _leave(" [ok]");
  835. return;
  836. }
  837. limit = rxrpc_reap_client_connections;
  838. spin_lock(&rxnet->client_conn_cache_lock);
  839. nr_active = rxnet->nr_active_client_conns;
  840. while (nr_active > limit) {
  841. ASSERT(!list_empty(&rxnet->active_client_conns));
  842. conn = list_entry(rxnet->active_client_conns.next,
  843. struct rxrpc_connection, cache_link);
  844. ASSERTIFCMP(conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE,
  845. conn->cache_state, ==, RXRPC_CONN_CLIENT_UPGRADE);
  846. if (list_empty(&conn->waiting_calls)) {
  847. trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
  848. conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
  849. list_del_init(&conn->cache_link);
  850. } else {
  851. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  852. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  853. list_move_tail(&conn->cache_link,
  854. &rxnet->waiting_client_conns);
  855. }
  856. nr_active--;
  857. }
  858. rxnet->nr_active_client_conns = nr_active;
  859. spin_unlock(&rxnet->client_conn_cache_lock);
  860. ASSERTCMP(nr_active, >=, 0);
  861. _leave(" [culled]");
  862. }
  863. /*
  864. * Discard expired client connections from the idle list. Each conn in the
  865. * idle list has been exposed and holds an extra ref because of that.
  866. *
  867. * This may be called from conn setup or from a work item so cannot be
  868. * considered non-reentrant.
  869. */
  870. void rxrpc_discard_expired_client_conns(struct work_struct *work)
  871. {
  872. struct rxrpc_connection *conn;
  873. struct rxrpc_net *rxnet =
  874. container_of(to_delayed_work(work),
  875. struct rxrpc_net, client_conn_reaper);
  876. unsigned long expiry, conn_expires_at, now;
  877. unsigned int nr_conns;
  878. bool did_discard = false;
  879. _enter("");
  880. if (list_empty(&rxnet->idle_client_conns)) {
  881. _leave(" [empty]");
  882. return;
  883. }
  884. /* Don't double up on the discarding */
  885. if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
  886. _leave(" [already]");
  887. return;
  888. }
  889. /* We keep an estimate of what the number of conns ought to be after
  890. * we've discarded some so that we don't overdo the discarding.
  891. */
  892. nr_conns = rxnet->nr_client_conns;
  893. next:
  894. spin_lock(&rxnet->client_conn_cache_lock);
  895. if (list_empty(&rxnet->idle_client_conns))
  896. goto out;
  897. conn = list_entry(rxnet->idle_client_conns.next,
  898. struct rxrpc_connection, cache_link);
  899. ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
  900. if (!rxnet->kill_all_client_conns) {
  901. /* If the number of connections is over the reap limit, we
  902. * expedite discard by reducing the expiry timeout. We must,
  903. * however, have at least a short grace period to be able to do
  904. * final-ACK or ABORT retransmission.
  905. */
  906. expiry = rxrpc_conn_idle_client_expiry;
  907. if (nr_conns > rxrpc_reap_client_connections)
  908. expiry = rxrpc_conn_idle_client_fast_expiry;
  909. conn_expires_at = conn->idle_timestamp + expiry;
  910. now = READ_ONCE(jiffies);
  911. if (time_after(conn_expires_at, now))
  912. goto not_yet_expired;
  913. }
  914. trace_rxrpc_client(conn, -1, rxrpc_client_discard);
  915. if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
  916. BUG();
  917. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  918. list_del_init(&conn->cache_link);
  919. spin_unlock(&rxnet->client_conn_cache_lock);
  920. /* When we cleared the EXPOSED flag, we took on responsibility for the
  921. * reference that that had on the usage count. We deal with that here.
  922. * If someone re-sets the flag and re-gets the ref, that's fine.
  923. */
  924. rxrpc_put_connection(conn);
  925. did_discard = true;
  926. nr_conns--;
  927. goto next;
  928. not_yet_expired:
  929. /* The connection at the front of the queue hasn't yet expired, so
  930. * schedule the work item for that point if we discarded something.
  931. *
  932. * We don't worry if the work item is already scheduled - it can look
  933. * after rescheduling itself at a later time. We could cancel it, but
  934. * then things get messier.
  935. */
  936. _debug("not yet");
  937. if (!rxnet->kill_all_client_conns)
  938. queue_delayed_work(rxrpc_workqueue,
  939. &rxnet->client_conn_reaper,
  940. conn_expires_at - now);
  941. out:
  942. spin_unlock(&rxnet->client_conn_cache_lock);
  943. spin_unlock(&rxnet->client_conn_discard_lock);
  944. _leave("");
  945. }
  946. /*
  947. * Preemptively destroy all the client connection records rather than waiting
  948. * for them to time out
  949. */
  950. void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
  951. {
  952. _enter("");
  953. spin_lock(&rxnet->client_conn_cache_lock);
  954. rxnet->kill_all_client_conns = true;
  955. spin_unlock(&rxnet->client_conn_cache_lock);
  956. cancel_delayed_work(&rxnet->client_conn_reaper);
  957. if (!queue_delayed_work(rxrpc_workqueue, &rxnet->client_conn_reaper, 0))
  958. _debug("destroy: queue failed");
  959. _leave("");
  960. }