conn_client.c 31 KB

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