threads.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/random.h>
  35. #include "rds.h"
  36. /*
  37. * All of connection management is simplified by serializing it through
  38. * work queues that execute in a connection managing thread.
  39. *
  40. * TCP wants to send acks through sendpage() in response to data_ready(),
  41. * but it needs a process context to do so.
  42. *
  43. * The receive paths need to allocate but can't drop packets (!) so we have
  44. * a thread around to block allocating if the receive fast path sees an
  45. * allocation failure.
  46. */
  47. /* Grand Unified Theory of connection life cycle:
  48. * At any point in time, the connection can be in one of these states:
  49. * DOWN, CONNECTING, UP, DISCONNECTING, ERROR
  50. *
  51. * The following transitions are possible:
  52. * ANY -> ERROR
  53. * UP -> DISCONNECTING
  54. * ERROR -> DISCONNECTING
  55. * DISCONNECTING -> DOWN
  56. * DOWN -> CONNECTING
  57. * CONNECTING -> UP
  58. *
  59. * Transition to state DISCONNECTING/DOWN:
  60. * - Inside the shutdown worker; synchronizes with xmit path
  61. * through c_send_lock, and with connection management callbacks
  62. * via c_cm_lock.
  63. *
  64. * For receive callbacks, we rely on the underlying transport
  65. * (TCP, IB/RDMA) to provide the necessary synchronisation.
  66. */
  67. struct workqueue_struct *rds_wq;
  68. void rds_connect_complete(struct rds_connection *conn)
  69. {
  70. if (!rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_UP)) {
  71. printk(KERN_WARNING "%s: Cannot transition to state UP, "
  72. "current state is %d\n",
  73. __func__,
  74. atomic_read(&conn->c_state));
  75. atomic_set(&conn->c_state, RDS_CONN_ERROR);
  76. queue_work(rds_wq, &conn->c_down_w);
  77. return;
  78. }
  79. rdsdebug("conn %p for %pI4 to %pI4 complete\n",
  80. conn, &conn->c_laddr, &conn->c_faddr);
  81. conn->c_reconnect_jiffies = 0;
  82. set_bit(0, &conn->c_map_queued);
  83. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  84. queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
  85. }
  86. /*
  87. * This random exponential backoff is relied on to eventually resolve racing
  88. * connects.
  89. *
  90. * If connect attempts race then both parties drop both connections and come
  91. * here to wait for a random amount of time before trying again. Eventually
  92. * the backoff range will be so much greater than the time it takes to
  93. * establish a connection that one of the pair will establish the connection
  94. * before the other's random delay fires.
  95. *
  96. * Connection attempts that arrive while a connection is already established
  97. * are also considered to be racing connects. This lets a connection from
  98. * a rebooted machine replace an existing stale connection before the transport
  99. * notices that the connection has failed.
  100. *
  101. * We should *always* start with a random backoff; otherwise a broken connection
  102. * will always take several iterations to be re-established.
  103. */
  104. static void rds_queue_reconnect(struct rds_connection *conn)
  105. {
  106. unsigned long rand;
  107. rdsdebug("conn %p for %pI4 to %pI4 reconnect jiffies %lu\n",
  108. conn, &conn->c_laddr, &conn->c_faddr,
  109. conn->c_reconnect_jiffies);
  110. set_bit(RDS_RECONNECT_PENDING, &conn->c_flags);
  111. if (conn->c_reconnect_jiffies == 0) {
  112. conn->c_reconnect_jiffies = rds_sysctl_reconnect_min_jiffies;
  113. queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
  114. return;
  115. }
  116. get_random_bytes(&rand, sizeof(rand));
  117. rdsdebug("%lu delay %lu ceil conn %p for %pI4 -> %pI4\n",
  118. rand % conn->c_reconnect_jiffies, conn->c_reconnect_jiffies,
  119. conn, &conn->c_laddr, &conn->c_faddr);
  120. queue_delayed_work(rds_wq, &conn->c_conn_w,
  121. rand % conn->c_reconnect_jiffies);
  122. conn->c_reconnect_jiffies = min(conn->c_reconnect_jiffies * 2,
  123. rds_sysctl_reconnect_max_jiffies);
  124. }
  125. void rds_connect_worker(struct work_struct *work)
  126. {
  127. struct rds_connection *conn = container_of(work, struct rds_connection, c_conn_w.work);
  128. int ret;
  129. clear_bit(RDS_RECONNECT_PENDING, &conn->c_flags);
  130. if (rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  131. ret = conn->c_trans->conn_connect(conn);
  132. rdsdebug("conn %p for %pI4 to %pI4 dispatched, ret %d\n",
  133. conn, &conn->c_laddr, &conn->c_faddr, ret);
  134. if (ret) {
  135. if (rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_DOWN))
  136. rds_queue_reconnect(conn);
  137. else
  138. rds_conn_error(conn, "RDS: connect failed\n");
  139. }
  140. }
  141. }
  142. void rds_shutdown_worker(struct work_struct *work)
  143. {
  144. struct rds_connection *conn = container_of(work, struct rds_connection, c_down_w);
  145. /* shut it down unless it's down already */
  146. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
  147. /*
  148. * Quiesce the connection mgmt handlers before we start tearing
  149. * things down. We don't hold the mutex for the entire
  150. * duration of the shutdown operation, else we may be
  151. * deadlocking with the CM handler. Instead, the CM event
  152. * handler is supposed to check for state DISCONNECTING
  153. */
  154. mutex_lock(&conn->c_cm_lock);
  155. if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING)
  156. && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) {
  157. rds_conn_error(conn, "shutdown called in state %d\n",
  158. atomic_read(&conn->c_state));
  159. mutex_unlock(&conn->c_cm_lock);
  160. return;
  161. }
  162. mutex_unlock(&conn->c_cm_lock);
  163. mutex_lock(&conn->c_send_lock);
  164. conn->c_trans->conn_shutdown(conn);
  165. rds_conn_reset(conn);
  166. mutex_unlock(&conn->c_send_lock);
  167. if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) {
  168. /* This can happen - eg when we're in the middle of tearing
  169. * down the connection, and someone unloads the rds module.
  170. * Quite reproduceable with loopback connections.
  171. * Mostly harmless.
  172. */
  173. rds_conn_error(conn,
  174. "%s: failed to transition to state DOWN, "
  175. "current state is %d\n",
  176. __func__,
  177. atomic_read(&conn->c_state));
  178. return;
  179. }
  180. }
  181. /* Then reconnect if it's still live.
  182. * The passive side of an IB loopback connection is never added
  183. * to the conn hash, so we never trigger a reconnect on this
  184. * conn - the reconnect is always triggered by the active peer. */
  185. cancel_delayed_work(&conn->c_conn_w);
  186. if (!hlist_unhashed(&conn->c_hash_node))
  187. rds_queue_reconnect(conn);
  188. }
  189. void rds_send_worker(struct work_struct *work)
  190. {
  191. struct rds_connection *conn = container_of(work, struct rds_connection, c_send_w.work);
  192. int ret;
  193. if (rds_conn_state(conn) == RDS_CONN_UP) {
  194. ret = rds_send_xmit(conn);
  195. rdsdebug("conn %p ret %d\n", conn, ret);
  196. switch (ret) {
  197. case -EAGAIN:
  198. rds_stats_inc(s_send_immediate_retry);
  199. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  200. break;
  201. case -ENOMEM:
  202. rds_stats_inc(s_send_delayed_retry);
  203. queue_delayed_work(rds_wq, &conn->c_send_w, 2);
  204. default:
  205. break;
  206. }
  207. }
  208. }
  209. void rds_recv_worker(struct work_struct *work)
  210. {
  211. struct rds_connection *conn = container_of(work, struct rds_connection, c_recv_w.work);
  212. int ret;
  213. if (rds_conn_state(conn) == RDS_CONN_UP) {
  214. ret = conn->c_trans->recv(conn);
  215. rdsdebug("conn %p ret %d\n", conn, ret);
  216. switch (ret) {
  217. case -EAGAIN:
  218. rds_stats_inc(s_recv_immediate_retry);
  219. queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
  220. break;
  221. case -ENOMEM:
  222. rds_stats_inc(s_recv_delayed_retry);
  223. queue_delayed_work(rds_wq, &conn->c_recv_w, 2);
  224. default:
  225. break;
  226. }
  227. }
  228. }
  229. void rds_threads_exit(void)
  230. {
  231. destroy_workqueue(rds_wq);
  232. }
  233. int __init rds_threads_init(void)
  234. {
  235. rds_wq = create_singlethread_workqueue("krdsd");
  236. if (rds_wq == NULL)
  237. return -ENOMEM;
  238. return 0;
  239. }