cong.c 13 KB

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