18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 58c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 118c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 128c2ecf20Sopenharmony_ci * conditions are met: 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 158c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 168c2ecf20Sopenharmony_ci * disclaimer. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 198c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 208c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 218c2ecf20Sopenharmony_ci * provided with the distribution. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 308c2ecf20Sopenharmony_ci * SOFTWARE. 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci#include <linux/slab.h> 348c2ecf20Sopenharmony_ci#include <linux/types.h> 358c2ecf20Sopenharmony_ci#include <linux/rbtree.h> 368c2ecf20Sopenharmony_ci#include <linux/bitops.h> 378c2ecf20Sopenharmony_ci#include <linux/export.h> 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#include "rds.h" 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* 428c2ecf20Sopenharmony_ci * This file implements the receive side of the unconventional congestion 438c2ecf20Sopenharmony_ci * management in RDS. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * Messages waiting in the receive queue on the receiving socket are accounted 468c2ecf20Sopenharmony_ci * against the sockets SO_RCVBUF option value. Only the payload bytes in the 478c2ecf20Sopenharmony_ci * message are accounted for. If the number of bytes queued equals or exceeds 488c2ecf20Sopenharmony_ci * rcvbuf then the socket is congested. All sends attempted to this socket's 498c2ecf20Sopenharmony_ci * address should return block or return -EWOULDBLOCK. 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * Applications are expected to be reasonably tuned such that this situation 528c2ecf20Sopenharmony_ci * very rarely occurs. An application encountering this "back-pressure" is 538c2ecf20Sopenharmony_ci * considered a bug. 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * This is implemented by having each node maintain bitmaps which indicate 568c2ecf20Sopenharmony_ci * which ports on bound addresses are congested. As the bitmap changes it is 578c2ecf20Sopenharmony_ci * sent through all the connections which terminate in the local address of the 588c2ecf20Sopenharmony_ci * bitmap which changed. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * The bitmaps are allocated as connections are brought up. This avoids 618c2ecf20Sopenharmony_ci * allocation in the interrupt handling path which queues messages on sockets. 628c2ecf20Sopenharmony_ci * The dense bitmaps let transports send the entire bitmap on any bitmap change 638c2ecf20Sopenharmony_ci * reasonably efficiently. This is much easier to implement than some 648c2ecf20Sopenharmony_ci * finer-grained communication of per-port congestion. The sender does a very 658c2ecf20Sopenharmony_ci * inexpensive bit test to test if the port it's about to send to is congested 668c2ecf20Sopenharmony_ci * or not. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* 708c2ecf20Sopenharmony_ci * Interaction with poll is a tad tricky. We want all processes stuck in 718c2ecf20Sopenharmony_ci * poll to wake up and check whether a congested destination became uncongested. 728c2ecf20Sopenharmony_ci * The really sad thing is we have no idea which destinations the application 738c2ecf20Sopenharmony_ci * wants to send to - we don't even know which rds_connections are involved. 748c2ecf20Sopenharmony_ci * So until we implement a more flexible rds poll interface, we have to make 758c2ecf20Sopenharmony_ci * do with this: 768c2ecf20Sopenharmony_ci * We maintain a global counter that is incremented each time a congestion map 778c2ecf20Sopenharmony_ci * update is received. Each rds socket tracks this value, and if rds_poll 788c2ecf20Sopenharmony_ci * finds that the saved generation number is smaller than the global generation 798c2ecf20Sopenharmony_ci * number, it wakes up the process. 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_cistatic atomic_t rds_cong_generation = ATOMIC_INIT(0); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* 848c2ecf20Sopenharmony_ci * Congestion monitoring 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_cistatic LIST_HEAD(rds_cong_monitor); 878c2ecf20Sopenharmony_cistatic DEFINE_RWLOCK(rds_cong_monitor_lock); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci/* 908c2ecf20Sopenharmony_ci * Yes, a global lock. It's used so infrequently that it's worth keeping it 918c2ecf20Sopenharmony_ci * global to simplify the locking. It's only used in the following 928c2ecf20Sopenharmony_ci * circumstances: 938c2ecf20Sopenharmony_ci * 948c2ecf20Sopenharmony_ci * - on connection buildup to associate a conn with its maps 958c2ecf20Sopenharmony_ci * - on map changes to inform conns of a new map to send 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * It's sadly ordered under the socket callback lock and the connection lock. 988c2ecf20Sopenharmony_ci * Receive paths can mark ports congested from interrupt context so the 998c2ecf20Sopenharmony_ci * lock masks interrupts. 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(rds_cong_lock); 1028c2ecf20Sopenharmony_cistatic struct rb_root rds_cong_tree = RB_ROOT; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic struct rds_cong_map *rds_cong_tree_walk(const struct in6_addr *addr, 1058c2ecf20Sopenharmony_ci struct rds_cong_map *insert) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci struct rb_node **p = &rds_cong_tree.rb_node; 1088c2ecf20Sopenharmony_ci struct rb_node *parent = NULL; 1098c2ecf20Sopenharmony_ci struct rds_cong_map *map; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci while (*p) { 1128c2ecf20Sopenharmony_ci int diff; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci parent = *p; 1158c2ecf20Sopenharmony_ci map = rb_entry(parent, struct rds_cong_map, m_rb_node); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci diff = rds_addr_cmp(addr, &map->m_addr); 1188c2ecf20Sopenharmony_ci if (diff < 0) 1198c2ecf20Sopenharmony_ci p = &(*p)->rb_left; 1208c2ecf20Sopenharmony_ci else if (diff > 0) 1218c2ecf20Sopenharmony_ci p = &(*p)->rb_right; 1228c2ecf20Sopenharmony_ci else 1238c2ecf20Sopenharmony_ci return map; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci if (insert) { 1278c2ecf20Sopenharmony_ci rb_link_node(&insert->m_rb_node, parent, p); 1288c2ecf20Sopenharmony_ci rb_insert_color(&insert->m_rb_node, &rds_cong_tree); 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci return NULL; 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci/* 1348c2ecf20Sopenharmony_ci * There is only ever one bitmap for any address. Connections try and allocate 1358c2ecf20Sopenharmony_ci * these bitmaps in the process getting pointers to them. The bitmaps are only 1368c2ecf20Sopenharmony_ci * ever freed as the module is removed after all connections have been freed. 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_cistatic struct rds_cong_map *rds_cong_from_addr(const struct in6_addr *addr) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct rds_cong_map *map; 1418c2ecf20Sopenharmony_ci struct rds_cong_map *ret = NULL; 1428c2ecf20Sopenharmony_ci unsigned long zp; 1438c2ecf20Sopenharmony_ci unsigned long i; 1448c2ecf20Sopenharmony_ci unsigned long flags; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci map = kzalloc(sizeof(struct rds_cong_map), GFP_KERNEL); 1478c2ecf20Sopenharmony_ci if (!map) 1488c2ecf20Sopenharmony_ci return NULL; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci map->m_addr = *addr; 1518c2ecf20Sopenharmony_ci init_waitqueue_head(&map->m_waitq); 1528c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&map->m_conn_list); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci for (i = 0; i < RDS_CONG_MAP_PAGES; i++) { 1558c2ecf20Sopenharmony_ci zp = get_zeroed_page(GFP_KERNEL); 1568c2ecf20Sopenharmony_ci if (zp == 0) 1578c2ecf20Sopenharmony_ci goto out; 1588c2ecf20Sopenharmony_ci map->m_page_addrs[i] = zp; 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci spin_lock_irqsave(&rds_cong_lock, flags); 1628c2ecf20Sopenharmony_ci ret = rds_cong_tree_walk(addr, map); 1638c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rds_cong_lock, flags); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (!ret) { 1668c2ecf20Sopenharmony_ci ret = map; 1678c2ecf20Sopenharmony_ci map = NULL; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciout: 1718c2ecf20Sopenharmony_ci if (map) { 1728c2ecf20Sopenharmony_ci for (i = 0; i < RDS_CONG_MAP_PAGES && map->m_page_addrs[i]; i++) 1738c2ecf20Sopenharmony_ci free_page(map->m_page_addrs[i]); 1748c2ecf20Sopenharmony_ci kfree(map); 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci rdsdebug("map %p for addr %pI6c\n", ret, addr); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci return ret; 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci/* 1838c2ecf20Sopenharmony_ci * Put the conn on its local map's list. This is called when the conn is 1848c2ecf20Sopenharmony_ci * really added to the hash. It's nested under the rds_conn_lock, sadly. 1858c2ecf20Sopenharmony_ci */ 1868c2ecf20Sopenharmony_civoid rds_cong_add_conn(struct rds_connection *conn) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci unsigned long flags; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci rdsdebug("conn %p now on map %p\n", conn, conn->c_lcong); 1918c2ecf20Sopenharmony_ci spin_lock_irqsave(&rds_cong_lock, flags); 1928c2ecf20Sopenharmony_ci list_add_tail(&conn->c_map_item, &conn->c_lcong->m_conn_list); 1938c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rds_cong_lock, flags); 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_civoid rds_cong_remove_conn(struct rds_connection *conn) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci unsigned long flags; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci rdsdebug("removing conn %p from map %p\n", conn, conn->c_lcong); 2018c2ecf20Sopenharmony_ci spin_lock_irqsave(&rds_cong_lock, flags); 2028c2ecf20Sopenharmony_ci list_del_init(&conn->c_map_item); 2038c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rds_cong_lock, flags); 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ciint rds_cong_get_maps(struct rds_connection *conn) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci conn->c_lcong = rds_cong_from_addr(&conn->c_laddr); 2098c2ecf20Sopenharmony_ci conn->c_fcong = rds_cong_from_addr(&conn->c_faddr); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (!(conn->c_lcong && conn->c_fcong)) 2128c2ecf20Sopenharmony_ci return -ENOMEM; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci return 0; 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_civoid rds_cong_queue_updates(struct rds_cong_map *map) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci struct rds_connection *conn; 2208c2ecf20Sopenharmony_ci unsigned long flags; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci spin_lock_irqsave(&rds_cong_lock, flags); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci list_for_each_entry(conn, &map->m_conn_list, c_map_item) { 2258c2ecf20Sopenharmony_ci struct rds_conn_path *cp = &conn->c_path[0]; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci rcu_read_lock(); 2288c2ecf20Sopenharmony_ci if (!test_and_set_bit(0, &conn->c_map_queued) && 2298c2ecf20Sopenharmony_ci !rds_destroy_pending(cp->cp_conn)) { 2308c2ecf20Sopenharmony_ci rds_stats_inc(s_cong_update_queued); 2318c2ecf20Sopenharmony_ci /* We cannot inline the call to rds_send_xmit() here 2328c2ecf20Sopenharmony_ci * for two reasons (both pertaining to a TCP transport): 2338c2ecf20Sopenharmony_ci * 1. When we get here from the receive path, we 2348c2ecf20Sopenharmony_ci * are already holding the sock_lock (held by 2358c2ecf20Sopenharmony_ci * tcp_v4_rcv()). So inlining calls to 2368c2ecf20Sopenharmony_ci * tcp_setsockopt and/or tcp_sendmsg will deadlock 2378c2ecf20Sopenharmony_ci * when it tries to get the sock_lock()) 2388c2ecf20Sopenharmony_ci * 2. Interrupts are masked so that we can mark the 2398c2ecf20Sopenharmony_ci * port congested from both send and recv paths. 2408c2ecf20Sopenharmony_ci * (See comment around declaration of rdc_cong_lock). 2418c2ecf20Sopenharmony_ci * An attempt to get the sock_lock() here will 2428c2ecf20Sopenharmony_ci * therefore trigger warnings. 2438c2ecf20Sopenharmony_ci * Defer the xmit to rds_send_worker() instead. 2448c2ecf20Sopenharmony_ci */ 2458c2ecf20Sopenharmony_ci queue_delayed_work(rds_wq, &cp->cp_send_w, 0); 2468c2ecf20Sopenharmony_ci } 2478c2ecf20Sopenharmony_ci rcu_read_unlock(); 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rds_cong_lock, flags); 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_civoid rds_cong_map_updated(struct rds_cong_map *map, uint64_t portmask) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci rdsdebug("waking map %p for %pI4\n", 2568c2ecf20Sopenharmony_ci map, &map->m_addr); 2578c2ecf20Sopenharmony_ci rds_stats_inc(s_cong_update_received); 2588c2ecf20Sopenharmony_ci atomic_inc(&rds_cong_generation); 2598c2ecf20Sopenharmony_ci if (waitqueue_active(&map->m_waitq)) 2608c2ecf20Sopenharmony_ci wake_up(&map->m_waitq); 2618c2ecf20Sopenharmony_ci if (waitqueue_active(&rds_poll_waitq)) 2628c2ecf20Sopenharmony_ci wake_up_all(&rds_poll_waitq); 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci if (portmask && !list_empty(&rds_cong_monitor)) { 2658c2ecf20Sopenharmony_ci unsigned long flags; 2668c2ecf20Sopenharmony_ci struct rds_sock *rs; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci read_lock_irqsave(&rds_cong_monitor_lock, flags); 2698c2ecf20Sopenharmony_ci list_for_each_entry(rs, &rds_cong_monitor, rs_cong_list) { 2708c2ecf20Sopenharmony_ci spin_lock(&rs->rs_lock); 2718c2ecf20Sopenharmony_ci rs->rs_cong_notify |= (rs->rs_cong_mask & portmask); 2728c2ecf20Sopenharmony_ci rs->rs_cong_mask &= ~portmask; 2738c2ecf20Sopenharmony_ci spin_unlock(&rs->rs_lock); 2748c2ecf20Sopenharmony_ci if (rs->rs_cong_notify) 2758c2ecf20Sopenharmony_ci rds_wake_sk_sleep(rs); 2768c2ecf20Sopenharmony_ci } 2778c2ecf20Sopenharmony_ci read_unlock_irqrestore(&rds_cong_monitor_lock, flags); 2788c2ecf20Sopenharmony_ci } 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rds_cong_map_updated); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciint rds_cong_updated_since(unsigned long *recent) 2838c2ecf20Sopenharmony_ci{ 2848c2ecf20Sopenharmony_ci unsigned long gen = atomic_read(&rds_cong_generation); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if (likely(*recent == gen)) 2878c2ecf20Sopenharmony_ci return 0; 2888c2ecf20Sopenharmony_ci *recent = gen; 2898c2ecf20Sopenharmony_ci return 1; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci/* 2938c2ecf20Sopenharmony_ci * We're called under the locking that protects the sockets receive buffer 2948c2ecf20Sopenharmony_ci * consumption. This makes it a lot easier for the caller to only call us 2958c2ecf20Sopenharmony_ci * when it knows that an existing set bit needs to be cleared, and vice versa. 2968c2ecf20Sopenharmony_ci * We can't block and we need to deal with concurrent sockets working against 2978c2ecf20Sopenharmony_ci * the same per-address map. 2988c2ecf20Sopenharmony_ci */ 2998c2ecf20Sopenharmony_civoid rds_cong_set_bit(struct rds_cong_map *map, __be16 port) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci unsigned long i; 3028c2ecf20Sopenharmony_ci unsigned long off; 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci rdsdebug("setting congestion for %pI4:%u in map %p\n", 3058c2ecf20Sopenharmony_ci &map->m_addr, ntohs(port), map); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci i = be16_to_cpu(port) / RDS_CONG_MAP_PAGE_BITS; 3088c2ecf20Sopenharmony_ci off = be16_to_cpu(port) % RDS_CONG_MAP_PAGE_BITS; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci set_bit_le(off, (void *)map->m_page_addrs[i]); 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_civoid rds_cong_clear_bit(struct rds_cong_map *map, __be16 port) 3148c2ecf20Sopenharmony_ci{ 3158c2ecf20Sopenharmony_ci unsigned long i; 3168c2ecf20Sopenharmony_ci unsigned long off; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci rdsdebug("clearing congestion for %pI4:%u in map %p\n", 3198c2ecf20Sopenharmony_ci &map->m_addr, ntohs(port), map); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci i = be16_to_cpu(port) / RDS_CONG_MAP_PAGE_BITS; 3228c2ecf20Sopenharmony_ci off = be16_to_cpu(port) % RDS_CONG_MAP_PAGE_BITS; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci clear_bit_le(off, (void *)map->m_page_addrs[i]); 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic int rds_cong_test_bit(struct rds_cong_map *map, __be16 port) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci unsigned long i; 3308c2ecf20Sopenharmony_ci unsigned long off; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci i = be16_to_cpu(port) / RDS_CONG_MAP_PAGE_BITS; 3338c2ecf20Sopenharmony_ci off = be16_to_cpu(port) % RDS_CONG_MAP_PAGE_BITS; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci return test_bit_le(off, (void *)map->m_page_addrs[i]); 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_civoid rds_cong_add_socket(struct rds_sock *rs) 3398c2ecf20Sopenharmony_ci{ 3408c2ecf20Sopenharmony_ci unsigned long flags; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci write_lock_irqsave(&rds_cong_monitor_lock, flags); 3438c2ecf20Sopenharmony_ci if (list_empty(&rs->rs_cong_list)) 3448c2ecf20Sopenharmony_ci list_add(&rs->rs_cong_list, &rds_cong_monitor); 3458c2ecf20Sopenharmony_ci write_unlock_irqrestore(&rds_cong_monitor_lock, flags); 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_civoid rds_cong_remove_socket(struct rds_sock *rs) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci unsigned long flags; 3518c2ecf20Sopenharmony_ci struct rds_cong_map *map; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci write_lock_irqsave(&rds_cong_monitor_lock, flags); 3548c2ecf20Sopenharmony_ci list_del_init(&rs->rs_cong_list); 3558c2ecf20Sopenharmony_ci write_unlock_irqrestore(&rds_cong_monitor_lock, flags); 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci /* update congestion map for now-closed port */ 3588c2ecf20Sopenharmony_ci spin_lock_irqsave(&rds_cong_lock, flags); 3598c2ecf20Sopenharmony_ci map = rds_cong_tree_walk(&rs->rs_bound_addr, NULL); 3608c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rds_cong_lock, flags); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci if (map && rds_cong_test_bit(map, rs->rs_bound_port)) { 3638c2ecf20Sopenharmony_ci rds_cong_clear_bit(map, rs->rs_bound_port); 3648c2ecf20Sopenharmony_ci rds_cong_queue_updates(map); 3658c2ecf20Sopenharmony_ci } 3668c2ecf20Sopenharmony_ci} 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ciint rds_cong_wait(struct rds_cong_map *map, __be16 port, int nonblock, 3698c2ecf20Sopenharmony_ci struct rds_sock *rs) 3708c2ecf20Sopenharmony_ci{ 3718c2ecf20Sopenharmony_ci if (!rds_cong_test_bit(map, port)) 3728c2ecf20Sopenharmony_ci return 0; 3738c2ecf20Sopenharmony_ci if (nonblock) { 3748c2ecf20Sopenharmony_ci if (rs && rs->rs_cong_monitor) { 3758c2ecf20Sopenharmony_ci unsigned long flags; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci /* It would have been nice to have an atomic set_bit on 3788c2ecf20Sopenharmony_ci * a uint64_t. */ 3798c2ecf20Sopenharmony_ci spin_lock_irqsave(&rs->rs_lock, flags); 3808c2ecf20Sopenharmony_ci rs->rs_cong_mask |= RDS_CONG_MONITOR_MASK(ntohs(port)); 3818c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&rs->rs_lock, flags); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci /* Test again - a congestion update may have arrived in 3848c2ecf20Sopenharmony_ci * the meantime. */ 3858c2ecf20Sopenharmony_ci if (!rds_cong_test_bit(map, port)) 3868c2ecf20Sopenharmony_ci return 0; 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci rds_stats_inc(s_cong_send_error); 3898c2ecf20Sopenharmony_ci return -ENOBUFS; 3908c2ecf20Sopenharmony_ci } 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci rds_stats_inc(s_cong_send_blocked); 3938c2ecf20Sopenharmony_ci rdsdebug("waiting on map %p for port %u\n", map, be16_to_cpu(port)); 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci return wait_event_interruptible(map->m_waitq, 3968c2ecf20Sopenharmony_ci !rds_cong_test_bit(map, port)); 3978c2ecf20Sopenharmony_ci} 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_civoid rds_cong_exit(void) 4008c2ecf20Sopenharmony_ci{ 4018c2ecf20Sopenharmony_ci struct rb_node *node; 4028c2ecf20Sopenharmony_ci struct rds_cong_map *map; 4038c2ecf20Sopenharmony_ci unsigned long i; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci while ((node = rb_first(&rds_cong_tree))) { 4068c2ecf20Sopenharmony_ci map = rb_entry(node, struct rds_cong_map, m_rb_node); 4078c2ecf20Sopenharmony_ci rdsdebug("freeing map %p\n", map); 4088c2ecf20Sopenharmony_ci rb_erase(&map->m_rb_node, &rds_cong_tree); 4098c2ecf20Sopenharmony_ci for (i = 0; i < RDS_CONG_MAP_PAGES && map->m_page_addrs[i]; i++) 4108c2ecf20Sopenharmony_ci free_page(map->m_page_addrs[i]); 4118c2ecf20Sopenharmony_ci kfree(map); 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci} 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci/* 4168c2ecf20Sopenharmony_ci * Allocate a RDS message containing a congestion update. 4178c2ecf20Sopenharmony_ci */ 4188c2ecf20Sopenharmony_cistruct rds_message *rds_cong_update_alloc(struct rds_connection *conn) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci struct rds_cong_map *map = conn->c_lcong; 4218c2ecf20Sopenharmony_ci struct rds_message *rm; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci rm = rds_message_map_pages(map->m_page_addrs, RDS_CONG_MAP_BYTES); 4248c2ecf20Sopenharmony_ci if (!IS_ERR(rm)) 4258c2ecf20Sopenharmony_ci rm->m_inc.i_hdr.h_flags = RDS_FLAG_CONG_BITMAP; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci return rm; 4288c2ecf20Sopenharmony_ci} 429