cong.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2007 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/types.h>
  34. #include <linux/rbtree.h>
  35. #include "rds.h"
  36. /*
  37. * This file implements the receive side of the unconventional congestion
  38. * management in RDS.
  39. *
  40. * Messages waiting in the receive queue on the receiving socket are accounted
  41. * against the sockets SO_RCVBUF option value. Only the payload bytes in the
  42. * message are accounted for. If the number of bytes queued equals or exceeds
  43. * rcvbuf then the socket is congested. All sends attempted to this socket's
  44. * address should return block or return -EWOULDBLOCK.
  45. *
  46. * Applications are expected to be reasonably tuned such that this situation
  47. * very rarely occurs. An application encountering this "back-pressure" is
  48. * considered a bug.
  49. *
  50. * This is implemented by having each node maintain bitmaps which indicate
  51. * which ports on bound addresses are congested. As the bitmap changes it is
  52. * sent through all the connections which terminate in the local address of the
  53. * bitmap which changed.
  54. *
  55. * The bitmaps are allocated as connections are brought up. This avoids
  56. * allocation in the interrupt handling path which queues messages on sockets.
  57. * The dense bitmaps let transports send the entire bitmap on any bitmap change
  58. * reasonably efficiently. This is much easier to implement than some
  59. * finer-grained communication of per-port congestion. The sender does a very
  60. * inexpensive bit test to test if the port it's about to send to is congested
  61. * or not.
  62. */
  63. /*
  64. * Interaction with poll is a tad tricky. We want all processes stuck in
  65. * poll to wake up and check whether a congested destination became uncongested.
  66. * The really sad thing is we have no idea which destinations the application
  67. * wants to send to - we don't even know which rds_connections are involved.
  68. * So until we implement a more flexible rds poll interface, we have to make
  69. * do with this:
  70. * We maintain a global counter that is incremented each time a congestion map
  71. * update is received. Each rds socket tracks this value, and if rds_poll
  72. * finds that the saved generation number is smaller than the global generation
  73. * number, it wakes up the process.
  74. */
  75. static atomic_t rds_cong_generation = ATOMIC_INIT(0);
  76. /*
  77. * Congestion monitoring
  78. */
  79. static LIST_HEAD(rds_cong_monitor);
  80. static DEFINE_RWLOCK(rds_cong_monitor_lock);
  81. /*
  82. * Yes, a global lock. It's used so infrequently that it's worth keeping it
  83. * global to simplify the locking. It's only used in the following
  84. * circumstances:
  85. *
  86. * - on connection buildup to associate a conn with its maps
  87. * - on map changes to inform conns of a new map to send
  88. *
  89. * It's sadly ordered under the socket callback lock and the connection lock.
  90. * Receive paths can mark ports congested from interrupt context so the
  91. * lock masks interrupts.
  92. */
  93. static DEFINE_SPINLOCK(rds_cong_lock);
  94. static struct rb_root rds_cong_tree = RB_ROOT;
  95. static struct rds_cong_map *rds_cong_tree_walk(__be32 addr,
  96. struct rds_cong_map *insert)
  97. {
  98. struct rb_node **p = &rds_cong_tree.rb_node;
  99. struct rb_node *parent = NULL;
  100. struct rds_cong_map *map;
  101. while (*p) {
  102. parent = *p;
  103. map = rb_entry(parent, struct rds_cong_map, m_rb_node);
  104. if (addr < map->m_addr)
  105. p = &(*p)->rb_left;
  106. else if (addr > map->m_addr)
  107. p = &(*p)->rb_right;
  108. else
  109. return map;
  110. }
  111. if (insert) {
  112. rb_link_node(&insert->m_rb_node, parent, p);
  113. rb_insert_color(&insert->m_rb_node, &rds_cong_tree);
  114. }
  115. return NULL;
  116. }
  117. /*
  118. * There is only ever one bitmap for any address. Connections try and allocate
  119. * these bitmaps in the process getting pointers to them. The bitmaps are only
  120. * ever freed as the module is removed after all connections have been freed.
  121. */
  122. static struct rds_cong_map *rds_cong_from_addr(__be32 addr)
  123. {
  124. struct rds_cong_map *map;
  125. struct rds_cong_map *ret = NULL;
  126. unsigned long zp;
  127. unsigned long i;
  128. unsigned long flags;
  129. map = kzalloc(sizeof(struct rds_cong_map), GFP_KERNEL);
  130. if (map == NULL)
  131. return NULL;
  132. map->m_addr = addr;
  133. init_waitqueue_head(&map->m_waitq);
  134. INIT_LIST_HEAD(&map->m_conn_list);
  135. for (i = 0; i < RDS_CONG_MAP_PAGES; i++) {
  136. zp = get_zeroed_page(GFP_KERNEL);
  137. if (zp == 0)
  138. goto out;
  139. map->m_page_addrs[i] = zp;
  140. }
  141. spin_lock_irqsave(&rds_cong_lock, flags);
  142. ret = rds_cong_tree_walk(addr, map);
  143. spin_unlock_irqrestore(&rds_cong_lock, flags);
  144. if (ret == NULL) {
  145. ret = map;
  146. map = NULL;
  147. }
  148. out:
  149. if (map) {
  150. for (i = 0; i < RDS_CONG_MAP_PAGES && map->m_page_addrs[i]; i++)
  151. free_page(map->m_page_addrs[i]);
  152. kfree(map);
  153. }
  154. rdsdebug("map %p for addr %x\n", ret, be32_to_cpu(addr));
  155. return ret;
  156. }
  157. /*
  158. * Put the conn on its local map's list. This is called when the conn is
  159. * really added to the hash. It's nested under the rds_conn_lock, sadly.
  160. */
  161. void rds_cong_add_conn(struct rds_connection *conn)
  162. {
  163. unsigned long flags;
  164. rdsdebug("conn %p now on map %p\n", conn, conn->c_lcong);
  165. spin_lock_irqsave(&rds_cong_lock, flags);
  166. list_add_tail(&conn->c_map_item, &conn->c_lcong->m_conn_list);
  167. spin_unlock_irqrestore(&rds_cong_lock, flags);
  168. }
  169. void rds_cong_remove_conn(struct rds_connection *conn)
  170. {
  171. unsigned long flags;
  172. rdsdebug("removing conn %p from map %p\n", conn, conn->c_lcong);
  173. spin_lock_irqsave(&rds_cong_lock, flags);
  174. list_del_init(&conn->c_map_item);
  175. spin_unlock_irqrestore(&rds_cong_lock, flags);
  176. }
  177. int rds_cong_get_maps(struct rds_connection *conn)
  178. {
  179. conn->c_lcong = rds_cong_from_addr(conn->c_laddr);
  180. conn->c_fcong = rds_cong_from_addr(conn->c_faddr);
  181. if (conn->c_lcong == NULL || conn->c_fcong == NULL)
  182. return -ENOMEM;
  183. return 0;
  184. }
  185. void rds_cong_queue_updates(struct rds_cong_map *map)
  186. {
  187. struct rds_connection *conn;
  188. unsigned long flags;
  189. spin_lock_irqsave(&rds_cong_lock, flags);
  190. list_for_each_entry(conn, &map->m_conn_list, c_map_item) {
  191. if (!test_and_set_bit(0, &conn->c_map_queued)) {
  192. rds_stats_inc(s_cong_update_queued);
  193. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  194. }
  195. }
  196. spin_unlock_irqrestore(&rds_cong_lock, flags);
  197. }
  198. void rds_cong_map_updated(struct rds_cong_map *map, uint64_t portmask)
  199. {
  200. rdsdebug("waking map %p for %pI4\n",
  201. map, &map->m_addr);
  202. rds_stats_inc(s_cong_update_received);
  203. atomic_inc(&rds_cong_generation);
  204. if (waitqueue_active(&map->m_waitq))
  205. wake_up(&map->m_waitq);
  206. if (waitqueue_active(&rds_poll_waitq))
  207. wake_up_all(&rds_poll_waitq);
  208. if (portmask && !list_empty(&rds_cong_monitor)) {
  209. unsigned long flags;
  210. struct rds_sock *rs;
  211. read_lock_irqsave(&rds_cong_monitor_lock, flags);
  212. list_for_each_entry(rs, &rds_cong_monitor, rs_cong_list) {
  213. spin_lock(&rs->rs_lock);
  214. rs->rs_cong_notify |= (rs->rs_cong_mask & portmask);
  215. rs->rs_cong_mask &= ~portmask;
  216. spin_unlock(&rs->rs_lock);
  217. if (rs->rs_cong_notify)
  218. rds_wake_sk_sleep(rs);
  219. }
  220. read_unlock_irqrestore(&rds_cong_monitor_lock, flags);
  221. }
  222. }
  223. int rds_cong_updated_since(unsigned long *recent)
  224. {
  225. unsigned long gen = atomic_read(&rds_cong_generation);
  226. if (likely(*recent == gen))
  227. return 0;
  228. *recent = gen;
  229. return 1;
  230. }
  231. /*
  232. * We're called under the locking that protects the sockets receive buffer
  233. * consumption. This makes it a lot easier for the caller to only call us
  234. * when it knows that an existing set bit needs to be cleared, and vice versa.
  235. * We can't block and we need to deal with concurrent sockets working against
  236. * the same per-address map.
  237. */
  238. void rds_cong_set_bit(struct rds_cong_map *map, __be16 port)
  239. {
  240. unsigned long i;
  241. unsigned long off;
  242. rdsdebug("setting congestion for %pI4:%u in map %p\n",
  243. &map->m_addr, ntohs(port), map);
  244. i = be16_to_cpu(port) / RDS_CONG_MAP_PAGE_BITS;
  245. off = be16_to_cpu(port) % RDS_CONG_MAP_PAGE_BITS;
  246. generic___set_le_bit(off, (void *)map->m_page_addrs[i]);
  247. }
  248. void rds_cong_clear_bit(struct rds_cong_map *map, __be16 port)
  249. {
  250. unsigned long i;
  251. unsigned long off;
  252. rdsdebug("clearing congestion for %pI4:%u in map %p\n",
  253. &map->m_addr, ntohs(port), map);
  254. i = be16_to_cpu(port) / RDS_CONG_MAP_PAGE_BITS;
  255. off = be16_to_cpu(port) % RDS_CONG_MAP_PAGE_BITS;
  256. generic___clear_le_bit(off, (void *)map->m_page_addrs[i]);
  257. }
  258. static int rds_cong_test_bit(struct rds_cong_map *map, __be16 port)
  259. {
  260. unsigned long i;
  261. unsigned long off;
  262. i = be16_to_cpu(port) / RDS_CONG_MAP_PAGE_BITS;
  263. off = be16_to_cpu(port) % RDS_CONG_MAP_PAGE_BITS;
  264. return generic_test_le_bit(off, (void *)map->m_page_addrs[i]);
  265. }
  266. void rds_cong_add_socket(struct rds_sock *rs)
  267. {
  268. unsigned long flags;
  269. write_lock_irqsave(&rds_cong_monitor_lock, flags);
  270. if (list_empty(&rs->rs_cong_list))
  271. list_add(&rs->rs_cong_list, &rds_cong_monitor);
  272. write_unlock_irqrestore(&rds_cong_monitor_lock, flags);
  273. }
  274. void rds_cong_remove_socket(struct rds_sock *rs)
  275. {
  276. unsigned long flags;
  277. struct rds_cong_map *map;
  278. write_lock_irqsave(&rds_cong_monitor_lock, flags);
  279. list_del_init(&rs->rs_cong_list);
  280. write_unlock_irqrestore(&rds_cong_monitor_lock, flags);
  281. /* update congestion map for now-closed port */
  282. spin_lock_irqsave(&rds_cong_lock, flags);
  283. map = rds_cong_tree_walk(rs->rs_bound_addr, NULL);
  284. spin_unlock_irqrestore(&rds_cong_lock, flags);
  285. if (map && rds_cong_test_bit(map, rs->rs_bound_port)) {
  286. rds_cong_clear_bit(map, rs->rs_bound_port);
  287. rds_cong_queue_updates(map);
  288. }
  289. }
  290. int rds_cong_wait(struct rds_cong_map *map, __be16 port, int nonblock,
  291. struct rds_sock *rs)
  292. {
  293. if (!rds_cong_test_bit(map, port))
  294. return 0;
  295. if (nonblock) {
  296. if (rs && rs->rs_cong_monitor) {
  297. unsigned long flags;
  298. /* It would have been nice to have an atomic set_bit on
  299. * a uint64_t. */
  300. spin_lock_irqsave(&rs->rs_lock, flags);
  301. rs->rs_cong_mask |= RDS_CONG_MONITOR_MASK(ntohs(port));
  302. spin_unlock_irqrestore(&rs->rs_lock, flags);
  303. /* Test again - a congestion update may have arrived in
  304. * the meantime. */
  305. if (!rds_cong_test_bit(map, port))
  306. return 0;
  307. }
  308. rds_stats_inc(s_cong_send_error);
  309. return -ENOBUFS;
  310. }
  311. rds_stats_inc(s_cong_send_blocked);
  312. rdsdebug("waiting on map %p for port %u\n", map, be16_to_cpu(port));
  313. return wait_event_interruptible(map->m_waitq,
  314. !rds_cong_test_bit(map, port));
  315. }
  316. void rds_cong_exit(void)
  317. {
  318. struct rb_node *node;
  319. struct rds_cong_map *map;
  320. unsigned long i;
  321. while ((node = rb_first(&rds_cong_tree))) {
  322. map = rb_entry(node, struct rds_cong_map, m_rb_node);
  323. rdsdebug("freeing map %p\n", map);
  324. rb_erase(&map->m_rb_node, &rds_cong_tree);
  325. for (i = 0; i < RDS_CONG_MAP_PAGES && map->m_page_addrs[i]; i++)
  326. free_page(map->m_page_addrs[i]);
  327. kfree(map);
  328. }
  329. }
  330. /*
  331. * Allocate a RDS message containing a congestion update.
  332. */
  333. struct rds_message *rds_cong_update_alloc(struct rds_connection *conn)
  334. {
  335. struct rds_cong_map *map = conn->c_lcong;
  336. struct rds_message *rm;
  337. rm = rds_message_map_pages(map->m_page_addrs, RDS_CONG_MAP_BYTES);
  338. if (!IS_ERR(rm))
  339. rm->m_inc.i_hdr.h_flags = RDS_FLAG_CONG_BITMAP;
  340. return rm;
  341. }