conn_client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Client connection-specific management code.
  3. *
  4. * Copyright (C) 2016, 2020 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. *
  7. * Client connections need to be cached for a little while after they've made a
  8. * call so as to handle retransmitted DATA packets in case the server didn't
  9. * receive the final ACK or terminating ABORT we sent it.
  10. *
  11. * There are flags of relevance to the cache:
  12. *
  13. * (2) DONT_REUSE - The connection should be discarded as soon as possible and
  14. * should not be reused. This is set when an exclusive connection is used
  15. * or a call ID counter overflows.
  16. *
  17. * The caching state may only be changed if the cache lock is held.
  18. *
  19. * There are two idle client connection expiry durations. If the total number
  20. * of connections is below the reap threshold, we use the normal duration; if
  21. * it's above, we use the fast duration.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/slab.h>
  25. #include <linux/idr.h>
  26. #include <linux/timer.h>
  27. #include <linux/sched/signal.h>
  28. #include "ar-internal.h"
  29. __read_mostly unsigned int rxrpc_reap_client_connections = 900;
  30. __read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
  31. __read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
  32. static void rxrpc_activate_bundle(struct rxrpc_bundle *bundle)
  33. {
  34. atomic_inc(&bundle->active);
  35. }
  36. /*
  37. * Release a connection ID for a client connection.
  38. */
  39. static void rxrpc_put_client_connection_id(struct rxrpc_local *local,
  40. struct rxrpc_connection *conn)
  41. {
  42. idr_remove(&local->conn_ids, conn->proto.cid >> RXRPC_CIDSHIFT);
  43. }
  44. /*
  45. * Destroy the client connection ID tree.
  46. */
  47. static void rxrpc_destroy_client_conn_ids(struct rxrpc_local *local)
  48. {
  49. struct rxrpc_connection *conn;
  50. int id;
  51. if (!idr_is_empty(&local->conn_ids)) {
  52. idr_for_each_entry(&local->conn_ids, conn, id) {
  53. pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
  54. conn, refcount_read(&conn->ref));
  55. }
  56. BUG();
  57. }
  58. idr_destroy(&local->conn_ids);
  59. }
  60. /*
  61. * Allocate a connection bundle.
  62. */
  63. static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_call *call,
  64. gfp_t gfp)
  65. {
  66. static atomic_t rxrpc_bundle_id;
  67. struct rxrpc_bundle *bundle;
  68. bundle = kzalloc(sizeof(*bundle), gfp);
  69. if (bundle) {
  70. bundle->local = call->local;
  71. bundle->peer = rxrpc_get_peer(call->peer, rxrpc_peer_get_bundle);
  72. bundle->key = key_get(call->key);
  73. bundle->security = call->security;
  74. bundle->exclusive = test_bit(RXRPC_CALL_EXCLUSIVE, &call->flags);
  75. bundle->upgrade = test_bit(RXRPC_CALL_UPGRADE, &call->flags);
  76. bundle->service_id = call->dest_srx.srx_service;
  77. bundle->security_level = call->security_level;
  78. bundle->debug_id = atomic_inc_return(&rxrpc_bundle_id);
  79. refcount_set(&bundle->ref, 1);
  80. atomic_set(&bundle->active, 1);
  81. INIT_LIST_HEAD(&bundle->waiting_calls);
  82. trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_new);
  83. write_lock(&bundle->local->rxnet->conn_lock);
  84. list_add_tail(&bundle->proc_link, &bundle->local->rxnet->bundle_proc_list);
  85. write_unlock(&bundle->local->rxnet->conn_lock);
  86. }
  87. return bundle;
  88. }
  89. struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle,
  90. enum rxrpc_bundle_trace why)
  91. {
  92. int r;
  93. __refcount_inc(&bundle->ref, &r);
  94. trace_rxrpc_bundle(bundle->debug_id, r + 1, why);
  95. return bundle;
  96. }
  97. static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
  98. {
  99. trace_rxrpc_bundle(bundle->debug_id, refcount_read(&bundle->ref),
  100. rxrpc_bundle_free);
  101. write_lock(&bundle->local->rxnet->conn_lock);
  102. list_del(&bundle->proc_link);
  103. write_unlock(&bundle->local->rxnet->conn_lock);
  104. rxrpc_put_peer(bundle->peer, rxrpc_peer_put_bundle);
  105. key_put(bundle->key);
  106. kfree(bundle);
  107. }
  108. void rxrpc_put_bundle(struct rxrpc_bundle *bundle, enum rxrpc_bundle_trace why)
  109. {
  110. unsigned int id;
  111. bool dead;
  112. int r;
  113. if (bundle) {
  114. id = bundle->debug_id;
  115. dead = __refcount_dec_and_test(&bundle->ref, &r);
  116. trace_rxrpc_bundle(id, r - 1, why);
  117. if (dead)
  118. rxrpc_free_bundle(bundle);
  119. }
  120. }
  121. /*
  122. * Get rid of outstanding client connection preallocations when a local
  123. * endpoint is destroyed.
  124. */
  125. void rxrpc_purge_client_connections(struct rxrpc_local *local)
  126. {
  127. rxrpc_destroy_client_conn_ids(local);
  128. }
  129. /*
  130. * Allocate a client connection.
  131. */
  132. static struct rxrpc_connection *
  133. rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle)
  134. {
  135. struct rxrpc_connection *conn;
  136. struct rxrpc_local *local = bundle->local;
  137. struct rxrpc_net *rxnet = local->rxnet;
  138. int id;
  139. _enter("");
  140. conn = rxrpc_alloc_connection(rxnet, GFP_ATOMIC | __GFP_NOWARN);
  141. if (!conn)
  142. return ERR_PTR(-ENOMEM);
  143. id = idr_alloc_cyclic(&local->conn_ids, conn, 1, 0x40000000,
  144. GFP_ATOMIC | __GFP_NOWARN);
  145. if (id < 0) {
  146. kfree(conn);
  147. return ERR_PTR(id);
  148. }
  149. refcount_set(&conn->ref, 1);
  150. conn->proto.cid = id << RXRPC_CIDSHIFT;
  151. conn->proto.epoch = local->rxnet->epoch;
  152. conn->out_clientflag = RXRPC_CLIENT_INITIATED;
  153. conn->bundle = rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_conn);
  154. conn->local = rxrpc_get_local(bundle->local, rxrpc_local_get_client_conn);
  155. conn->peer = rxrpc_get_peer(bundle->peer, rxrpc_peer_get_client_conn);
  156. conn->key = key_get(bundle->key);
  157. conn->security = bundle->security;
  158. conn->exclusive = bundle->exclusive;
  159. conn->upgrade = bundle->upgrade;
  160. conn->orig_service_id = bundle->service_id;
  161. conn->security_level = bundle->security_level;
  162. conn->state = RXRPC_CONN_CLIENT_UNSECURED;
  163. conn->service_id = conn->orig_service_id;
  164. if (conn->security == &rxrpc_no_security)
  165. conn->state = RXRPC_CONN_CLIENT;
  166. atomic_inc(&rxnet->nr_conns);
  167. write_lock(&rxnet->conn_lock);
  168. list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
  169. write_unlock(&rxnet->conn_lock);
  170. rxrpc_see_connection(conn, rxrpc_conn_new_client);
  171. atomic_inc(&rxnet->nr_client_conns);
  172. trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
  173. return conn;
  174. }
  175. /*
  176. * Determine if a connection may be reused.
  177. */
  178. static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
  179. {
  180. struct rxrpc_net *rxnet;
  181. int id_cursor, id, distance, limit;
  182. if (!conn)
  183. goto dont_reuse;
  184. rxnet = conn->rxnet;
  185. if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
  186. goto dont_reuse;
  187. if ((conn->state != RXRPC_CONN_CLIENT_UNSECURED &&
  188. conn->state != RXRPC_CONN_CLIENT) ||
  189. conn->proto.epoch != rxnet->epoch)
  190. goto mark_dont_reuse;
  191. /* The IDR tree gets very expensive on memory if the connection IDs are
  192. * widely scattered throughout the number space, so we shall want to
  193. * kill off connections that, say, have an ID more than about four
  194. * times the maximum number of client conns away from the current
  195. * allocation point to try and keep the IDs concentrated.
  196. */
  197. id_cursor = idr_get_cursor(&conn->local->conn_ids);
  198. id = conn->proto.cid >> RXRPC_CIDSHIFT;
  199. distance = id - id_cursor;
  200. if (distance < 0)
  201. distance = -distance;
  202. limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
  203. if (distance > limit)
  204. goto mark_dont_reuse;
  205. return true;
  206. mark_dont_reuse:
  207. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  208. dont_reuse:
  209. return false;
  210. }
  211. /*
  212. * Look up the conn bundle that matches the connection parameters, adding it if
  213. * it doesn't yet exist.
  214. */
  215. int rxrpc_look_up_bundle(struct rxrpc_call *call, gfp_t gfp)
  216. {
  217. struct rxrpc_bundle *bundle, *candidate;
  218. struct rxrpc_local *local = call->local;
  219. struct rb_node *p, **pp, *parent;
  220. long diff;
  221. bool upgrade = test_bit(RXRPC_CALL_UPGRADE, &call->flags);
  222. _enter("{%px,%x,%u,%u}",
  223. call->peer, key_serial(call->key), call->security_level,
  224. upgrade);
  225. if (test_bit(RXRPC_CALL_EXCLUSIVE, &call->flags)) {
  226. call->bundle = rxrpc_alloc_bundle(call, gfp);
  227. return call->bundle ? 0 : -ENOMEM;
  228. }
  229. /* First, see if the bundle is already there. */
  230. _debug("search 1");
  231. spin_lock(&local->client_bundles_lock);
  232. p = local->client_bundles.rb_node;
  233. while (p) {
  234. bundle = rb_entry(p, struct rxrpc_bundle, local_node);
  235. #define cmp(X, Y) ((long)(X) - (long)(Y))
  236. diff = (cmp(bundle->peer, call->peer) ?:
  237. cmp(bundle->key, call->key) ?:
  238. cmp(bundle->security_level, call->security_level) ?:
  239. cmp(bundle->upgrade, upgrade));
  240. #undef cmp
  241. if (diff < 0)
  242. p = p->rb_left;
  243. else if (diff > 0)
  244. p = p->rb_right;
  245. else
  246. goto found_bundle;
  247. }
  248. spin_unlock(&local->client_bundles_lock);
  249. _debug("not found");
  250. /* It wasn't. We need to add one. */
  251. candidate = rxrpc_alloc_bundle(call, gfp);
  252. if (!candidate)
  253. return -ENOMEM;
  254. _debug("search 2");
  255. spin_lock(&local->client_bundles_lock);
  256. pp = &local->client_bundles.rb_node;
  257. parent = NULL;
  258. while (*pp) {
  259. parent = *pp;
  260. bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
  261. #define cmp(X, Y) ((long)(X) - (long)(Y))
  262. diff = (cmp(bundle->peer, call->peer) ?:
  263. cmp(bundle->key, call->key) ?:
  264. cmp(bundle->security_level, call->security_level) ?:
  265. cmp(bundle->upgrade, upgrade));
  266. #undef cmp
  267. if (diff < 0)
  268. pp = &(*pp)->rb_left;
  269. else if (diff > 0)
  270. pp = &(*pp)->rb_right;
  271. else
  272. goto found_bundle_free;
  273. }
  274. _debug("new bundle");
  275. rb_link_node(&candidate->local_node, parent, pp);
  276. rb_insert_color(&candidate->local_node, &local->client_bundles);
  277. call->bundle = rxrpc_get_bundle(candidate, rxrpc_bundle_get_client_call);
  278. spin_unlock(&local->client_bundles_lock);
  279. _leave(" = B=%u [new]", call->bundle->debug_id);
  280. return 0;
  281. found_bundle_free:
  282. rxrpc_free_bundle(candidate);
  283. found_bundle:
  284. call->bundle = rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_call);
  285. rxrpc_activate_bundle(bundle);
  286. spin_unlock(&local->client_bundles_lock);
  287. _leave(" = B=%u [found]", call->bundle->debug_id);
  288. return 0;
  289. }
  290. /*
  291. * Allocate a new connection and add it into a bundle.
  292. */
  293. static bool rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle,
  294. unsigned int slot)
  295. {
  296. struct rxrpc_connection *conn, *old;
  297. unsigned int shift = slot * RXRPC_MAXCALLS;
  298. unsigned int i;
  299. old = bundle->conns[slot];
  300. if (old) {
  301. bundle->conns[slot] = NULL;
  302. bundle->conn_ids[slot] = 0;
  303. trace_rxrpc_client(old, -1, rxrpc_client_replace);
  304. rxrpc_put_connection(old, rxrpc_conn_put_noreuse);
  305. }
  306. conn = rxrpc_alloc_client_connection(bundle);
  307. if (IS_ERR(conn)) {
  308. bundle->alloc_error = PTR_ERR(conn);
  309. return false;
  310. }
  311. rxrpc_activate_bundle(bundle);
  312. conn->bundle_shift = shift;
  313. bundle->conns[slot] = conn;
  314. bundle->conn_ids[slot] = conn->debug_id;
  315. for (i = 0; i < RXRPC_MAXCALLS; i++)
  316. set_bit(shift + i, &bundle->avail_chans);
  317. return true;
  318. }
  319. /*
  320. * Add a connection to a bundle if there are no usable connections or we have
  321. * connections waiting for extra capacity.
  322. */
  323. static bool rxrpc_bundle_has_space(struct rxrpc_bundle *bundle)
  324. {
  325. int slot = -1, i, usable;
  326. _enter("");
  327. bundle->alloc_error = 0;
  328. /* See if there are any usable connections. */
  329. usable = 0;
  330. for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
  331. if (rxrpc_may_reuse_conn(bundle->conns[i]))
  332. usable++;
  333. else if (slot == -1)
  334. slot = i;
  335. }
  336. if (!usable && bundle->upgrade)
  337. bundle->try_upgrade = true;
  338. if (!usable)
  339. goto alloc_conn;
  340. if (!bundle->avail_chans &&
  341. !bundle->try_upgrade &&
  342. usable < ARRAY_SIZE(bundle->conns))
  343. goto alloc_conn;
  344. _leave("");
  345. return usable;
  346. alloc_conn:
  347. return slot >= 0 ? rxrpc_add_conn_to_bundle(bundle, slot) : false;
  348. }
  349. /*
  350. * Assign a channel to the call at the front of the queue and wake the call up.
  351. * We don't increment the callNumber counter until this number has been exposed
  352. * to the world.
  353. */
  354. static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
  355. unsigned int channel)
  356. {
  357. struct rxrpc_channel *chan = &conn->channels[channel];
  358. struct rxrpc_bundle *bundle = conn->bundle;
  359. struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
  360. struct rxrpc_call, wait_link);
  361. u32 call_id = chan->call_counter + 1;
  362. _enter("C=%x,%u", conn->debug_id, channel);
  363. list_del_init(&call->wait_link);
  364. trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
  365. /* Cancel the final ACK on the previous call if it hasn't been sent yet
  366. * as the DATA packet will implicitly ACK it.
  367. */
  368. clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  369. clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
  370. rxrpc_see_call(call, rxrpc_call_see_activate_client);
  371. call->conn = rxrpc_get_connection(conn, rxrpc_conn_get_activate_call);
  372. call->cid = conn->proto.cid | channel;
  373. call->call_id = call_id;
  374. call->dest_srx.srx_service = conn->service_id;
  375. call->cong_ssthresh = call->peer->cong_ssthresh;
  376. if (call->cong_cwnd >= call->cong_ssthresh)
  377. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  378. else
  379. call->cong_mode = RXRPC_CALL_SLOW_START;
  380. chan->call_id = call_id;
  381. chan->call_debug_id = call->debug_id;
  382. chan->call = call;
  383. rxrpc_see_call(call, rxrpc_call_see_connected);
  384. trace_rxrpc_connect_call(call);
  385. call->tx_last_sent = ktime_get_real();
  386. rxrpc_start_call_timer(call);
  387. rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_SEND_REQUEST);
  388. wake_up(&call->waitq);
  389. }
  390. /*
  391. * Remove a connection from the idle list if it's on it.
  392. */
  393. static void rxrpc_unidle_conn(struct rxrpc_connection *conn)
  394. {
  395. if (!list_empty(&conn->cache_link)) {
  396. list_del_init(&conn->cache_link);
  397. rxrpc_put_connection(conn, rxrpc_conn_put_unidle);
  398. }
  399. }
  400. /*
  401. * Assign channels and callNumbers to waiting calls.
  402. */
  403. static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
  404. {
  405. struct rxrpc_connection *conn;
  406. unsigned long avail, mask;
  407. unsigned int channel, slot;
  408. trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
  409. if (bundle->try_upgrade)
  410. mask = 1;
  411. else
  412. mask = ULONG_MAX;
  413. while (!list_empty(&bundle->waiting_calls)) {
  414. avail = bundle->avail_chans & mask;
  415. if (!avail)
  416. break;
  417. channel = __ffs(avail);
  418. clear_bit(channel, &bundle->avail_chans);
  419. slot = channel / RXRPC_MAXCALLS;
  420. conn = bundle->conns[slot];
  421. if (!conn)
  422. break;
  423. if (bundle->try_upgrade)
  424. set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  425. rxrpc_unidle_conn(conn);
  426. channel &= (RXRPC_MAXCALLS - 1);
  427. conn->act_chans |= 1 << channel;
  428. rxrpc_activate_one_channel(conn, channel);
  429. }
  430. }
  431. /*
  432. * Connect waiting channels (called from the I/O thread).
  433. */
  434. void rxrpc_connect_client_calls(struct rxrpc_local *local)
  435. {
  436. struct rxrpc_call *call;
  437. while ((call = list_first_entry_or_null(&local->new_client_calls,
  438. struct rxrpc_call, wait_link))
  439. ) {
  440. struct rxrpc_bundle *bundle = call->bundle;
  441. spin_lock(&local->client_call_lock);
  442. list_move_tail(&call->wait_link, &bundle->waiting_calls);
  443. rxrpc_see_call(call, rxrpc_call_see_waiting_call);
  444. spin_unlock(&local->client_call_lock);
  445. if (rxrpc_bundle_has_space(bundle))
  446. rxrpc_activate_channels(bundle);
  447. }
  448. }
  449. /*
  450. * Note that a call, and thus a connection, is about to be exposed to the
  451. * world.
  452. */
  453. void rxrpc_expose_client_call(struct rxrpc_call *call)
  454. {
  455. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  456. struct rxrpc_connection *conn = call->conn;
  457. struct rxrpc_channel *chan = &conn->channels[channel];
  458. if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  459. /* Mark the call ID as being used. If the callNumber counter
  460. * exceeds ~2 billion, we kill the connection after its
  461. * outstanding calls have finished so that the counter doesn't
  462. * wrap.
  463. */
  464. chan->call_counter++;
  465. if (chan->call_counter >= INT_MAX)
  466. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  467. trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
  468. spin_lock(&call->peer->lock);
  469. hlist_add_head(&call->error_link, &call->peer->error_targets);
  470. spin_unlock(&call->peer->lock);
  471. }
  472. }
  473. /*
  474. * Set the reap timer.
  475. */
  476. static void rxrpc_set_client_reap_timer(struct rxrpc_local *local)
  477. {
  478. if (!local->kill_all_client_conns) {
  479. unsigned long now = jiffies;
  480. unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
  481. if (local->rxnet->live)
  482. timer_reduce(&local->client_conn_reap_timer, reap_at);
  483. }
  484. }
  485. /*
  486. * Disconnect a client call.
  487. */
  488. void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
  489. {
  490. struct rxrpc_connection *conn;
  491. struct rxrpc_channel *chan = NULL;
  492. struct rxrpc_local *local = bundle->local;
  493. unsigned int channel;
  494. bool may_reuse;
  495. u32 cid;
  496. _enter("c=%x", call->debug_id);
  497. /* Calls that have never actually been assigned a channel can simply be
  498. * discarded.
  499. */
  500. conn = call->conn;
  501. if (!conn) {
  502. _debug("call is waiting");
  503. ASSERTCMP(call->call_id, ==, 0);
  504. ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
  505. /* May still be on ->new_client_calls. */
  506. spin_lock(&local->client_call_lock);
  507. list_del_init(&call->wait_link);
  508. spin_unlock(&local->client_call_lock);
  509. return;
  510. }
  511. cid = call->cid;
  512. channel = cid & RXRPC_CHANNELMASK;
  513. chan = &conn->channels[channel];
  514. trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
  515. if (WARN_ON(chan->call != call))
  516. return;
  517. may_reuse = rxrpc_may_reuse_conn(conn);
  518. /* If a client call was exposed to the world, we save the result for
  519. * retransmission.
  520. *
  521. * We use a barrier here so that the call number and abort code can be
  522. * read without needing to take a lock.
  523. *
  524. * TODO: Make the incoming packet handler check this and handle
  525. * terminal retransmission without requiring access to the call.
  526. */
  527. if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  528. _debug("exposed %u,%u", call->call_id, call->abort_code);
  529. __rxrpc_disconnect_call(conn, call);
  530. if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
  531. trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
  532. bundle->try_upgrade = false;
  533. if (may_reuse)
  534. rxrpc_activate_channels(bundle);
  535. }
  536. }
  537. /* See if we can pass the channel directly to another call. */
  538. if (may_reuse && !list_empty(&bundle->waiting_calls)) {
  539. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  540. rxrpc_activate_one_channel(conn, channel);
  541. return;
  542. }
  543. /* Schedule the final ACK to be transmitted in a short while so that it
  544. * can be skipped if we find a follow-on call. The first DATA packet
  545. * of the follow on call will implicitly ACK this call.
  546. */
  547. if (call->completion == RXRPC_CALL_SUCCEEDED &&
  548. test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  549. unsigned long final_ack_at = jiffies + 2;
  550. chan->final_ack_at = final_ack_at;
  551. smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
  552. set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  553. rxrpc_reduce_conn_timer(conn, final_ack_at);
  554. }
  555. /* Deactivate the channel. */
  556. chan->call = NULL;
  557. set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
  558. conn->act_chans &= ~(1 << channel);
  559. /* If no channels remain active, then put the connection on the idle
  560. * list for a short while. Give it a ref to stop it going away if it
  561. * becomes unbundled.
  562. */
  563. if (!conn->act_chans) {
  564. trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
  565. conn->idle_timestamp = jiffies;
  566. rxrpc_get_connection(conn, rxrpc_conn_get_idle);
  567. list_move_tail(&conn->cache_link, &local->idle_client_conns);
  568. rxrpc_set_client_reap_timer(local);
  569. }
  570. }
  571. /*
  572. * Remove a connection from a bundle.
  573. */
  574. static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
  575. {
  576. struct rxrpc_bundle *bundle = conn->bundle;
  577. unsigned int bindex;
  578. int i;
  579. _enter("C=%x", conn->debug_id);
  580. if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
  581. rxrpc_process_delayed_final_acks(conn, true);
  582. bindex = conn->bundle_shift / RXRPC_MAXCALLS;
  583. if (bundle->conns[bindex] == conn) {
  584. _debug("clear slot %u", bindex);
  585. bundle->conns[bindex] = NULL;
  586. bundle->conn_ids[bindex] = 0;
  587. for (i = 0; i < RXRPC_MAXCALLS; i++)
  588. clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
  589. rxrpc_put_client_connection_id(bundle->local, conn);
  590. rxrpc_deactivate_bundle(bundle);
  591. rxrpc_put_connection(conn, rxrpc_conn_put_unbundle);
  592. }
  593. }
  594. /*
  595. * Drop the active count on a bundle.
  596. */
  597. void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle)
  598. {
  599. struct rxrpc_local *local;
  600. bool need_put = false;
  601. if (!bundle)
  602. return;
  603. local = bundle->local;
  604. if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) {
  605. if (!bundle->exclusive) {
  606. _debug("erase bundle");
  607. rb_erase(&bundle->local_node, &local->client_bundles);
  608. need_put = true;
  609. }
  610. spin_unlock(&local->client_bundles_lock);
  611. if (need_put)
  612. rxrpc_put_bundle(bundle, rxrpc_bundle_put_discard);
  613. }
  614. }
  615. /*
  616. * Clean up a dead client connection.
  617. */
  618. void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
  619. {
  620. struct rxrpc_local *local = conn->local;
  621. struct rxrpc_net *rxnet = local->rxnet;
  622. _enter("C=%x", conn->debug_id);
  623. trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
  624. atomic_dec(&rxnet->nr_client_conns);
  625. rxrpc_put_client_connection_id(local, conn);
  626. }
  627. /*
  628. * Discard expired client connections from the idle list. Each conn in the
  629. * idle list has been exposed and holds an extra ref because of that.
  630. *
  631. * This may be called from conn setup or from a work item so cannot be
  632. * considered non-reentrant.
  633. */
  634. void rxrpc_discard_expired_client_conns(struct rxrpc_local *local)
  635. {
  636. struct rxrpc_connection *conn;
  637. unsigned long expiry, conn_expires_at, now;
  638. unsigned int nr_conns;
  639. _enter("");
  640. /* We keep an estimate of what the number of conns ought to be after
  641. * we've discarded some so that we don't overdo the discarding.
  642. */
  643. nr_conns = atomic_read(&local->rxnet->nr_client_conns);
  644. next:
  645. conn = list_first_entry_or_null(&local->idle_client_conns,
  646. struct rxrpc_connection, cache_link);
  647. if (!conn)
  648. return;
  649. if (!local->kill_all_client_conns) {
  650. /* If the number of connections is over the reap limit, we
  651. * expedite discard by reducing the expiry timeout. We must,
  652. * however, have at least a short grace period to be able to do
  653. * final-ACK or ABORT retransmission.
  654. */
  655. expiry = rxrpc_conn_idle_client_expiry;
  656. if (nr_conns > rxrpc_reap_client_connections)
  657. expiry = rxrpc_conn_idle_client_fast_expiry;
  658. if (conn->local->service_closed)
  659. expiry = rxrpc_closed_conn_expiry * HZ;
  660. conn_expires_at = conn->idle_timestamp + expiry;
  661. now = jiffies;
  662. if (time_after(conn_expires_at, now))
  663. goto not_yet_expired;
  664. }
  665. atomic_dec(&conn->active);
  666. trace_rxrpc_client(conn, -1, rxrpc_client_discard);
  667. list_del_init(&conn->cache_link);
  668. rxrpc_unbundle_conn(conn);
  669. /* Drop the ->cache_link ref */
  670. rxrpc_put_connection(conn, rxrpc_conn_put_discard_idle);
  671. nr_conns--;
  672. goto next;
  673. not_yet_expired:
  674. /* The connection at the front of the queue hasn't yet expired, so
  675. * schedule the work item for that point if we discarded something.
  676. *
  677. * We don't worry if the work item is already scheduled - it can look
  678. * after rescheduling itself at a later time. We could cancel it, but
  679. * then things get messier.
  680. */
  681. _debug("not yet");
  682. if (!local->kill_all_client_conns)
  683. timer_reduce(&local->client_conn_reap_timer, conn_expires_at);
  684. _leave("");
  685. }
  686. /*
  687. * Clean up the client connections on a local endpoint.
  688. */
  689. void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
  690. {
  691. struct rxrpc_connection *conn;
  692. _enter("");
  693. local->kill_all_client_conns = true;
  694. del_timer_sync(&local->client_conn_reap_timer);
  695. while ((conn = list_first_entry_or_null(&local->idle_client_conns,
  696. struct rxrpc_connection, cache_link))) {
  697. list_del_init(&conn->cache_link);
  698. atomic_dec(&conn->active);
  699. trace_rxrpc_client(conn, -1, rxrpc_client_discard);
  700. rxrpc_unbundle_conn(conn);
  701. rxrpc_put_connection(conn, rxrpc_conn_put_local_dead);
  702. }
  703. _leave(" [culled]");
  704. }