1 /* 1 /* 2 * Copyright (c) 2006, 2017 Oracle and/or its !! 2 * Copyright (c) 2006 Oracle. All rights reserved. 3 * 3 * 4 * This software is available to you under a c 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed un 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, ava 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this sourc 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 8 * OpenIB.org BSD license below: 9 * 9 * 10 * Redistribution and use in source and bi 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted pro 11 * without modification, are permitted provided that the following 12 * conditions are met: 12 * conditions are met: 13 * 13 * 14 * - Redistributions of source code must 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of condi 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 16 * disclaimer. 17 * 17 * 18 * - Redistributions in binary form must 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of condi 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/ 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 21 * provided with the distribution. 22 * 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT W 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMIT 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR P 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTH 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER L 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARIS 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 30 * SOFTWARE. 31 * 31 * 32 */ 32 */ 33 #include <linux/kernel.h> 33 #include <linux/kernel.h> 34 #include <linux/slab.h> 34 #include <linux/slab.h> 35 #include <net/tcp.h> 35 #include <net/tcp.h> 36 #include <trace/events/sock.h> << 37 36 38 #include "rds.h" 37 #include "rds.h" 39 #include "tcp.h" 38 #include "tcp.h" 40 39 41 static struct kmem_cache *rds_tcp_incoming_sla 40 static struct kmem_cache *rds_tcp_incoming_slab; 42 41 43 static void rds_tcp_inc_purge(struct rds_incom 42 static void rds_tcp_inc_purge(struct rds_incoming *inc) 44 { 43 { 45 struct rds_tcp_incoming *tinc; 44 struct rds_tcp_incoming *tinc; 46 tinc = container_of(inc, struct rds_tc 45 tinc = container_of(inc, struct rds_tcp_incoming, ti_inc); 47 rdsdebug("purging tinc %p inc %p\n", t 46 rdsdebug("purging tinc %p inc %p\n", tinc, inc); 48 skb_queue_purge(&tinc->ti_skb_list); 47 skb_queue_purge(&tinc->ti_skb_list); 49 } 48 } 50 49 51 void rds_tcp_inc_free(struct rds_incoming *inc 50 void rds_tcp_inc_free(struct rds_incoming *inc) 52 { 51 { 53 struct rds_tcp_incoming *tinc; 52 struct rds_tcp_incoming *tinc; 54 tinc = container_of(inc, struct rds_tc 53 tinc = container_of(inc, struct rds_tcp_incoming, ti_inc); 55 rds_tcp_inc_purge(inc); 54 rds_tcp_inc_purge(inc); 56 rdsdebug("freeing tinc %p inc %p\n", t 55 rdsdebug("freeing tinc %p inc %p\n", tinc, inc); 57 kmem_cache_free(rds_tcp_incoming_slab, 56 kmem_cache_free(rds_tcp_incoming_slab, tinc); 58 } 57 } 59 58 60 /* 59 /* 61 * this is pretty lame, but, whatever. 60 * this is pretty lame, but, whatever. 62 */ 61 */ 63 int rds_tcp_inc_copy_to_user(struct rds_incomi 62 int rds_tcp_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to) 64 { 63 { 65 struct rds_tcp_incoming *tinc; 64 struct rds_tcp_incoming *tinc; 66 struct sk_buff *skb; 65 struct sk_buff *skb; 67 int ret = 0; 66 int ret = 0; 68 67 69 if (!iov_iter_count(to)) 68 if (!iov_iter_count(to)) 70 goto out; 69 goto out; 71 70 72 tinc = container_of(inc, struct rds_tc 71 tinc = container_of(inc, struct rds_tcp_incoming, ti_inc); 73 72 74 skb_queue_walk(&tinc->ti_skb_list, skb 73 skb_queue_walk(&tinc->ti_skb_list, skb) { 75 unsigned long to_copy, skb_off 74 unsigned long to_copy, skb_off; 76 for (skb_off = 0; skb_off < sk 75 for (skb_off = 0; skb_off < skb->len; skb_off += to_copy) { 77 to_copy = iov_iter_cou 76 to_copy = iov_iter_count(to); 78 to_copy = min(to_copy, 77 to_copy = min(to_copy, skb->len - skb_off); 79 78 80 if (skb_copy_datagram_ 79 if (skb_copy_datagram_iter(skb, skb_off, to, to_copy)) 81 return -EFAULT 80 return -EFAULT; 82 81 83 rds_stats_add(s_copy_t 82 rds_stats_add(s_copy_to_user, to_copy); 84 ret += to_copy; 83 ret += to_copy; 85 84 86 if (!iov_iter_count(to 85 if (!iov_iter_count(to)) 87 goto out; 86 goto out; 88 } 87 } 89 } 88 } 90 out: 89 out: 91 return ret; 90 return ret; 92 } 91 } 93 92 94 /* 93 /* 95 * We have a series of skbs that have fragment 94 * We have a series of skbs that have fragmented pieces of the congestion 96 * bitmap. They must add up to the exact size 95 * bitmap. They must add up to the exact size of the congestion bitmap. We 97 * use the skb helpers to copy those into the 96 * use the skb helpers to copy those into the pages that make up the in-memory 98 * congestion bitmap for the remote address of 97 * congestion bitmap for the remote address of this connection. We then tell 99 * the congestion core that the bitmap has bee 98 * the congestion core that the bitmap has been changed so that it can wake up 100 * sleepers. 99 * sleepers. 101 * 100 * 102 * This is racing with sending paths which are 101 * This is racing with sending paths which are using test_bit to see if the 103 * bitmap indicates that their recipient is co 102 * bitmap indicates that their recipient is congested. 104 */ 103 */ 105 104 106 static void rds_tcp_cong_recv(struct rds_conne 105 static void rds_tcp_cong_recv(struct rds_connection *conn, 107 struct rds_tcp_i 106 struct rds_tcp_incoming *tinc) 108 { 107 { 109 struct sk_buff *skb; 108 struct sk_buff *skb; 110 unsigned int to_copy, skb_off; 109 unsigned int to_copy, skb_off; 111 unsigned int map_off; 110 unsigned int map_off; 112 unsigned int map_page; 111 unsigned int map_page; 113 struct rds_cong_map *map; 112 struct rds_cong_map *map; 114 int ret; 113 int ret; 115 114 116 /* catch completely corrupt packets */ 115 /* catch completely corrupt packets */ 117 if (be32_to_cpu(tinc->ti_inc.i_hdr.h_l 116 if (be32_to_cpu(tinc->ti_inc.i_hdr.h_len) != RDS_CONG_MAP_BYTES) 118 return; 117 return; 119 118 120 map_page = 0; 119 map_page = 0; 121 map_off = 0; 120 map_off = 0; 122 map = conn->c_fcong; 121 map = conn->c_fcong; 123 122 124 skb_queue_walk(&tinc->ti_skb_list, skb 123 skb_queue_walk(&tinc->ti_skb_list, skb) { 125 skb_off = 0; 124 skb_off = 0; 126 while (skb_off < skb->len) { 125 while (skb_off < skb->len) { 127 to_copy = min_t(unsign 126 to_copy = min_t(unsigned int, PAGE_SIZE - map_off, 128 skb->l 127 skb->len - skb_off); 129 128 130 BUG_ON(map_page >= RDS 129 BUG_ON(map_page >= RDS_CONG_MAP_PAGES); 131 130 132 /* only returns 0 or - 131 /* only returns 0 or -error */ 133 ret = skb_copy_bits(sk 132 ret = skb_copy_bits(skb, skb_off, 134 (void *)map->m 133 (void *)map->m_page_addrs[map_page] + map_off, 135 to_copy); 134 to_copy); 136 BUG_ON(ret != 0); 135 BUG_ON(ret != 0); 137 136 138 skb_off += to_copy; 137 skb_off += to_copy; 139 map_off += to_copy; 138 map_off += to_copy; 140 if (map_off == PAGE_SI 139 if (map_off == PAGE_SIZE) { 141 map_off = 0; 140 map_off = 0; 142 map_page++; 141 map_page++; 143 } 142 } 144 } 143 } 145 } 144 } 146 145 147 rds_cong_map_updated(map, ~(u64) 0); 146 rds_cong_map_updated(map, ~(u64) 0); 148 } 147 } 149 148 150 struct rds_tcp_desc_arg { 149 struct rds_tcp_desc_arg { 151 struct rds_conn_path *conn_path; 150 struct rds_conn_path *conn_path; 152 gfp_t gfp; 151 gfp_t gfp; 153 }; 152 }; 154 153 155 static int rds_tcp_data_recv(read_descriptor_t 154 static int rds_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb, 156 unsigned int offs 155 unsigned int offset, size_t len) 157 { 156 { 158 struct rds_tcp_desc_arg *arg = desc->a 157 struct rds_tcp_desc_arg *arg = desc->arg.data; 159 struct rds_conn_path *cp = arg->conn_p 158 struct rds_conn_path *cp = arg->conn_path; 160 struct rds_tcp_connection *tc = cp->cp 159 struct rds_tcp_connection *tc = cp->cp_transport_data; 161 struct rds_tcp_incoming *tinc = tc->t_ 160 struct rds_tcp_incoming *tinc = tc->t_tinc; 162 struct sk_buff *clone; 161 struct sk_buff *clone; 163 size_t left = len, to_copy; 162 size_t left = len, to_copy; 164 163 165 rdsdebug("tcp data tc %p skb %p offset 164 rdsdebug("tcp data tc %p skb %p offset %u len %zu\n", tc, skb, offset, 166 len); 165 len); 167 166 168 /* 167 /* 169 * tcp_read_sock() interprets partial 168 * tcp_read_sock() interprets partial progress as an indication to stop 170 * processing. 169 * processing. 171 */ 170 */ 172 while (left) { 171 while (left) { 173 if (!tinc) { 172 if (!tinc) { 174 tinc = kmem_cache_allo 173 tinc = kmem_cache_alloc(rds_tcp_incoming_slab, 175 174 arg->gfp); 176 if (!tinc) { 175 if (!tinc) { 177 desc->error = 176 desc->error = -ENOMEM; 178 goto out; 177 goto out; 179 } 178 } 180 tc->t_tinc = tinc; 179 tc->t_tinc = tinc; 181 rdsdebug("allocated ti !! 180 rdsdebug("alloced tinc %p\n", tinc); 182 rds_inc_path_init(&tin 181 rds_inc_path_init(&tinc->ti_inc, cp, 183 &cp- !! 182 cp->cp_conn->c_faddr); 184 tinc->ti_inc.i_rx_lat_ << 185 local_ << 186 << 187 /* 183 /* 188 * XXX * we might be a 184 * XXX * we might be able to use the __ variants when 189 * we've already seria 185 * we've already serialized at a higher level. 190 */ 186 */ 191 skb_queue_head_init(&t 187 skb_queue_head_init(&tinc->ti_skb_list); 192 } 188 } 193 189 194 if (left && tc->t_tinc_hdr_rem 190 if (left && tc->t_tinc_hdr_rem) { 195 to_copy = min(tc->t_ti 191 to_copy = min(tc->t_tinc_hdr_rem, left); 196 rdsdebug("copying %zu 192 rdsdebug("copying %zu header from skb %p\n", to_copy, 197 skb); 193 skb); 198 skb_copy_bits(skb, off 194 skb_copy_bits(skb, offset, 199 (char *) 195 (char *)&tinc->ti_inc.i_hdr + 200 196 sizeof(struct rds_header) - 201 197 tc->t_tinc_hdr_rem, 202 to_copy) 198 to_copy); 203 tc->t_tinc_hdr_rem -= 199 tc->t_tinc_hdr_rem -= to_copy; 204 left -= to_copy; 200 left -= to_copy; 205 offset += to_copy; 201 offset += to_copy; 206 202 207 if (tc->t_tinc_hdr_rem 203 if (tc->t_tinc_hdr_rem == 0) { 208 /* could be 0 204 /* could be 0 for a 0 len message */ 209 tc->t_tinc_dat 205 tc->t_tinc_data_rem = 210 be32_t 206 be32_to_cpu(tinc->ti_inc.i_hdr.h_len); 211 tinc->ti_inc.i << 212 local_ << 213 } 207 } 214 } 208 } 215 209 216 if (left && tc->t_tinc_data_re 210 if (left && tc->t_tinc_data_rem) { 217 to_copy = min(tc->t_ti 211 to_copy = min(tc->t_tinc_data_rem, left); 218 212 219 clone = pskb_extract(s 213 clone = pskb_extract(skb, offset, to_copy, arg->gfp); 220 if (!clone) { 214 if (!clone) { 221 desc->error = 215 desc->error = -ENOMEM; 222 goto out; 216 goto out; 223 } 217 } 224 218 225 skb_queue_tail(&tinc-> 219 skb_queue_tail(&tinc->ti_skb_list, clone); 226 220 227 rdsdebug("skb %p data 221 rdsdebug("skb %p data %p len %d off %u to_copy %zu -> " 228 "clone %p dat 222 "clone %p data %p len %d\n", 229 skb, skb->dat 223 skb, skb->data, skb->len, offset, to_copy, 230 clone, clone- 224 clone, clone->data, clone->len); 231 225 232 tc->t_tinc_data_rem -= 226 tc->t_tinc_data_rem -= to_copy; 233 left -= to_copy; 227 left -= to_copy; 234 offset += to_copy; 228 offset += to_copy; 235 } 229 } 236 230 237 if (tc->t_tinc_hdr_rem == 0 && 231 if (tc->t_tinc_hdr_rem == 0 && tc->t_tinc_data_rem == 0) { 238 struct rds_connection 232 struct rds_connection *conn = cp->cp_conn; 239 233 240 if (tinc->ti_inc.i_hdr 234 if (tinc->ti_inc.i_hdr.h_flags == RDS_FLAG_CONG_BITMAP) 241 rds_tcp_cong_r 235 rds_tcp_cong_recv(conn, tinc); 242 else 236 else 243 rds_recv_incom !! 237 rds_recv_incoming(conn, conn->c_faddr, 244 !! 238 conn->c_laddr, &tinc->ti_inc, 245 << 246 239 arg->gfp); 247 240 248 tc->t_tinc_hdr_rem = s 241 tc->t_tinc_hdr_rem = sizeof(struct rds_header); 249 tc->t_tinc_data_rem = 242 tc->t_tinc_data_rem = 0; 250 tc->t_tinc = NULL; 243 tc->t_tinc = NULL; 251 rds_inc_put(&tinc->ti_ 244 rds_inc_put(&tinc->ti_inc); 252 tinc = NULL; 245 tinc = NULL; 253 } 246 } 254 } 247 } 255 out: 248 out: 256 rdsdebug("returning len %zu left %zu s 249 rdsdebug("returning len %zu left %zu skb len %d rx queue depth %d\n", 257 len, left, skb->len, 250 len, left, skb->len, 258 skb_queue_len(&tc->t_sock->sk 251 skb_queue_len(&tc->t_sock->sk->sk_receive_queue)); 259 return len - left; 252 return len - left; 260 } 253 } 261 254 262 /* the caller has to hold the sock lock */ 255 /* the caller has to hold the sock lock */ 263 static int rds_tcp_read_sock(struct rds_conn_p 256 static int rds_tcp_read_sock(struct rds_conn_path *cp, gfp_t gfp) 264 { 257 { 265 struct rds_tcp_connection *tc = cp->cp 258 struct rds_tcp_connection *tc = cp->cp_transport_data; 266 struct socket *sock = tc->t_sock; 259 struct socket *sock = tc->t_sock; 267 read_descriptor_t desc; 260 read_descriptor_t desc; 268 struct rds_tcp_desc_arg arg; 261 struct rds_tcp_desc_arg arg; 269 262 270 /* It's like glib in the kernel! */ 263 /* It's like glib in the kernel! */ 271 arg.conn_path = cp; 264 arg.conn_path = cp; 272 arg.gfp = gfp; 265 arg.gfp = gfp; 273 desc.arg.data = &arg; 266 desc.arg.data = &arg; 274 desc.error = 0; 267 desc.error = 0; 275 desc.count = 1; /* give more than one 268 desc.count = 1; /* give more than one skb per call */ 276 269 277 tcp_read_sock(sock->sk, &desc, rds_tcp 270 tcp_read_sock(sock->sk, &desc, rds_tcp_data_recv); 278 rdsdebug("tcp_read_sock for tc %p gfp 271 rdsdebug("tcp_read_sock for tc %p gfp 0x%x returned %d\n", tc, gfp, 279 desc.error); 272 desc.error); 280 273 281 return desc.error; 274 return desc.error; 282 } 275 } 283 276 284 /* 277 /* 285 * We hold the sock lock to serialize our rds_ 278 * We hold the sock lock to serialize our rds_tcp_recv->tcp_read_sock from 286 * data_ready. 279 * data_ready. 287 * 280 * 288 * if we fail to allocate we're in trouble.. b 281 * if we fail to allocate we're in trouble.. blindly wait some time before 289 * trying again to see if the VM can free up s 282 * trying again to see if the VM can free up something for us. 290 */ 283 */ 291 int rds_tcp_recv_path(struct rds_conn_path *cp 284 int rds_tcp_recv_path(struct rds_conn_path *cp) 292 { 285 { 293 struct rds_tcp_connection *tc = cp->cp 286 struct rds_tcp_connection *tc = cp->cp_transport_data; 294 struct socket *sock = tc->t_sock; 287 struct socket *sock = tc->t_sock; 295 int ret = 0; 288 int ret = 0; 296 289 297 rdsdebug("recv worker path [%d] tc %p 290 rdsdebug("recv worker path [%d] tc %p sock %p\n", 298 cp->cp_index, tc, sock); 291 cp->cp_index, tc, sock); 299 292 300 lock_sock(sock->sk); 293 lock_sock(sock->sk); 301 ret = rds_tcp_read_sock(cp, GFP_KERNEL 294 ret = rds_tcp_read_sock(cp, GFP_KERNEL); 302 release_sock(sock->sk); 295 release_sock(sock->sk); 303 296 304 return ret; 297 return ret; 305 } 298 } 306 299 307 void rds_tcp_data_ready(struct sock *sk) 300 void rds_tcp_data_ready(struct sock *sk) 308 { 301 { 309 void (*ready)(struct sock *sk); 302 void (*ready)(struct sock *sk); 310 struct rds_conn_path *cp; 303 struct rds_conn_path *cp; 311 struct rds_tcp_connection *tc; 304 struct rds_tcp_connection *tc; 312 305 313 trace_sk_data_ready(sk); << 314 rdsdebug("data ready sk %p\n", sk); 306 rdsdebug("data ready sk %p\n", sk); 315 307 316 read_lock_bh(&sk->sk_callback_lock); 308 read_lock_bh(&sk->sk_callback_lock); 317 cp = sk->sk_user_data; 309 cp = sk->sk_user_data; 318 if (!cp) { /* check for teardown race 310 if (!cp) { /* check for teardown race */ 319 ready = sk->sk_data_ready; 311 ready = sk->sk_data_ready; 320 goto out; 312 goto out; 321 } 313 } 322 314 323 tc = cp->cp_transport_data; 315 tc = cp->cp_transport_data; 324 ready = tc->t_orig_data_ready; 316 ready = tc->t_orig_data_ready; 325 rds_tcp_stats_inc(s_tcp_data_ready_cal 317 rds_tcp_stats_inc(s_tcp_data_ready_calls); 326 318 327 if (rds_tcp_read_sock(cp, GFP_ATOMIC) !! 319 if (rds_tcp_read_sock(cp, GFP_ATOMIC) == -ENOMEM) 328 rcu_read_lock(); !! 320 queue_delayed_work(rds_wq, &cp->cp_recv_w, 0); 329 if (!rds_destroy_pending(cp->c << 330 queue_delayed_work(rds << 331 rcu_read_unlock(); << 332 } << 333 out: 321 out: 334 read_unlock_bh(&sk->sk_callback_lock); 322 read_unlock_bh(&sk->sk_callback_lock); 335 ready(sk); 323 ready(sk); 336 } 324 } 337 325 338 int rds_tcp_recv_init(void) 326 int rds_tcp_recv_init(void) 339 { 327 { 340 rds_tcp_incoming_slab = KMEM_CACHE(rds !! 328 rds_tcp_incoming_slab = kmem_cache_create("rds_tcp_incoming", >> 329 sizeof(struct rds_tcp_incoming), >> 330 0, 0, NULL); 341 if (!rds_tcp_incoming_slab) 331 if (!rds_tcp_incoming_slab) 342 return -ENOMEM; 332 return -ENOMEM; 343 return 0; 333 return 0; 344 } 334 } 345 335 346 void rds_tcp_recv_exit(void) 336 void rds_tcp_recv_exit(void) 347 { 337 { 348 kmem_cache_destroy(rds_tcp_incoming_sl 338 kmem_cache_destroy(rds_tcp_incoming_slab); 349 } 339 } 350 340
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.