1 /* 2 * Copyright (c) 2006, 2020 Oracle and/or its affiliates. 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/slab.h> 35 #include <linux/export.h> 36 #include <linux/skbuff.h> 37 #include <linux/list.h> 38 #include <linux/errqueue.h> 39 40 #include "rds.h" 41 42 static unsigned int rds_exthdr_size[__RDS_EXTHDR_MAX] = { 43 [RDS_EXTHDR_NONE] = 0, 44 [RDS_EXTHDR_VERSION] = sizeof(struct rds_ext_header_version), 45 [RDS_EXTHDR_RDMA] = sizeof(struct rds_ext_header_rdma), 46 [RDS_EXTHDR_RDMA_DEST] = sizeof(struct rds_ext_header_rdma_dest), 47 [RDS_EXTHDR_RDMA_BYTES] = sizeof(struct rds_ext_header_rdma_bytes), 48 [RDS_EXTHDR_NPATHS] = sizeof(__be16), 49 [RDS_EXTHDR_GEN_NUM] = sizeof(__be32), 50 [RDS_EXTHDR_SPORT_IDX] = 1, 51 }; 52 53 void rds_message_addref(struct rds_message *rm) 54 { 55 rdsdebug("addref rm %p ref %d\n", rm, refcount_read(&rm->m_refcount)); 56 refcount_inc(&rm->m_refcount); 57 } 58 EXPORT_SYMBOL_GPL(rds_message_addref); 59 60 static inline bool rds_zcookie_add(struct rds_msg_zcopy_info *info, u32 cookie) 61 { 62 struct rds_zcopy_cookies *ck = &info->zcookies; 63 int ncookies = ck->num; 64 65 if (ncookies == RDS_MAX_ZCOOKIES) 66 return false; 67 ck->cookies[ncookies] = cookie; 68 ck->num = ++ncookies; 69 return true; 70 } 71 72 static struct rds_msg_zcopy_info *rds_info_from_znotifier(struct rds_znotifier *znotif) 73 { 74 return container_of(znotif, struct rds_msg_zcopy_info, znotif); 75 } 76 77 void rds_notify_msg_zcopy_purge(struct rds_msg_zcopy_queue *q) 78 { 79 unsigned long flags; 80 LIST_HEAD(copy); 81 struct rds_msg_zcopy_info *info, *tmp; 82 83 spin_lock_irqsave(&q->lock, flags); 84 list_splice(&q->zcookie_head, ©); 85 INIT_LIST_HEAD(&q->zcookie_head); 86 spin_unlock_irqrestore(&q->lock, flags); 87 88 list_for_each_entry_safe(info, tmp, ©, rs_zcookie_next) { 89 list_del(&info->rs_zcookie_next); 90 kfree(info); 91 } 92 } 93 94 static void rds_rm_zerocopy_callback(struct rds_sock *rs, 95 struct rds_znotifier *znotif) 96 { 97 struct rds_msg_zcopy_info *info; 98 struct rds_msg_zcopy_queue *q; 99 u32 cookie = znotif->z_cookie; 100 struct rds_zcopy_cookies *ck; 101 struct list_head *head; 102 unsigned long flags; 103 104 mm_unaccount_pinned_pages(&znotif->z_mmp); 105 q = &rs->rs_zcookie_queue; 106 spin_lock_irqsave(&q->lock, flags); 107 head = &q->zcookie_head; 108 if (!list_empty(head)) { 109 info = list_first_entry(head, struct rds_msg_zcopy_info, 110 rs_zcookie_next); 111 if (rds_zcookie_add(info, cookie)) { 112 spin_unlock_irqrestore(&q->lock, flags); 113 kfree(rds_info_from_znotifier(znotif)); 114 /* caller invokes rds_wake_sk_sleep() */ 115 return; 116 } 117 } 118 119 info = rds_info_from_znotifier(znotif); 120 ck = &info->zcookies; 121 memset(ck, 0, sizeof(*ck)); 122 WARN_ON(!rds_zcookie_add(info, cookie)); 123 list_add_tail(&info->rs_zcookie_next, &q->zcookie_head); 124 125 spin_unlock_irqrestore(&q->lock, flags); 126 /* caller invokes rds_wake_sk_sleep() */ 127 } 128 129 /* 130 * This relies on dma_map_sg() not touching sg[].page during merging. 131 */ 132 static void rds_message_purge(struct rds_message *rm) 133 { 134 unsigned long i, flags; 135 bool zcopy = false; 136 137 if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags))) 138 return; 139 140 spin_lock_irqsave(&rm->m_rs_lock, flags); 141 if (rm->m_rs) { 142 struct rds_sock *rs = rm->m_rs; 143 144 if (rm->data.op_mmp_znotifier) { 145 zcopy = true; 146 rds_rm_zerocopy_callback(rs, rm->data.op_mmp_znotifier); 147 rds_wake_sk_sleep(rs); 148 rm->data.op_mmp_znotifier = NULL; 149 } 150 sock_put(rds_rs_to_sk(rs)); 151 rm->m_rs = NULL; 152 } 153 spin_unlock_irqrestore(&rm->m_rs_lock, flags); 154 155 for (i = 0; i < rm->data.op_nents; i++) { 156 /* XXX will have to put_page for page refs */ 157 if (!zcopy) 158 __free_page(sg_page(&rm->data.op_sg[i])); 159 else 160 put_page(sg_page(&rm->data.op_sg[i])); 161 } 162 rm->data.op_nents = 0; 163 164 if (rm->rdma.op_active) 165 rds_rdma_free_op(&rm->rdma); 166 if (rm->rdma.op_rdma_mr) 167 kref_put(&rm->rdma.op_rdma_mr->r_kref, __rds_put_mr_final); 168 169 if (rm->atomic.op_active) 170 rds_atomic_free_op(&rm->atomic); 171 if (rm->atomic.op_rdma_mr) 172 kref_put(&rm->atomic.op_rdma_mr->r_kref, __rds_put_mr_final); 173 } 174 175 void rds_message_put(struct rds_message *rm) 176 { 177 rdsdebug("put rm %p ref %d\n", rm, refcount_read(&rm->m_refcount)); 178 WARN(!refcount_read(&rm->m_refcount), "danger refcount zero on %p\n", rm); 179 if (refcount_dec_and_test(&rm->m_refcount)) { 180 BUG_ON(!list_empty(&rm->m_sock_item)); 181 BUG_ON(!list_empty(&rm->m_conn_item)); 182 rds_message_purge(rm); 183 184 kfree(rm); 185 } 186 } 187 EXPORT_SYMBOL_GPL(rds_message_put); 188 189 void rds_message_populate_header(struct rds_header *hdr, __be16 sport, 190 __be16 dport, u64 seq) 191 { 192 hdr->h_flags = 0; 193 hdr->h_sport = sport; 194 hdr->h_dport = dport; 195 hdr->h_sequence = cpu_to_be64(seq); 196 /* see rds_find_next_ext_space for reason why we memset the 197 * ext header 198 */ 199 memset(hdr->h_exthdr, RDS_EXTHDR_NONE, RDS_HEADER_EXT_SPACE); 200 } 201 EXPORT_SYMBOL_GPL(rds_message_populate_header); 202 203 /* 204 * Find the next place we can add an RDS header extension with 205 * specific length. Extension headers are pushed one after the 206 * other. In the following, the number after the colon is the number 207 * of bytes: 208 * 209 * [ type1:1 dta1:len1 [ type2:1 dta2:len2 ] ... ] RDS_EXTHDR_NONE 210 * 211 * If the extension headers fill the complete extension header space 212 * (16 bytes), the trailing RDS_EXTHDR_NONE is omitted. 213 */ 214 static int rds_find_next_ext_space(struct rds_header *hdr, unsigned int len, 215 u8 **ext_start) 216 { 217 unsigned int ext_len; 218 unsigned int type; 219 int ind = 0; 220 221 while ((ind + 1 + len) <= RDS_HEADER_EXT_SPACE) { 222 if (hdr->h_exthdr[ind] == RDS_EXTHDR_NONE) { 223 *ext_start = hdr->h_exthdr + ind; 224 return 0; 225 } 226 227 type = hdr->h_exthdr[ind]; 228 229 ext_len = (type < __RDS_EXTHDR_MAX) ? rds_exthdr_size[type] : 0; 230 WARN_ONCE(!ext_len, "Unknown ext hdr type %d\n", type); 231 if (!ext_len) 232 return -EINVAL; 233 234 /* ind points to a valid ext hdr with known length */ 235 ind += 1 + ext_len; 236 } 237 238 /* no room for extension */ 239 return -ENOSPC; 240 } 241 242 /* The ext hdr space is prefilled with zero from the kzalloc() */ 243 int rds_message_add_extension(struct rds_header *hdr, 244 unsigned int type, const void *data) 245 { 246 unsigned char *dst; 247 unsigned int len; 248 249 len = (type < __RDS_EXTHDR_MAX) ? rds_exthdr_size[type] : 0; 250 if (!len) 251 return 0; 252 253 if (rds_find_next_ext_space(hdr, len, &dst)) 254 return 0; 255 256 *dst++ = type; 257 memcpy(dst, data, len); 258 259 return 1; 260 } 261 EXPORT_SYMBOL_GPL(rds_message_add_extension); 262 263 /* 264 * If a message has extension headers, retrieve them here. 265 * Call like this: 266 * 267 * unsigned int pos = 0; 268 * 269 * while (1) { 270 * buflen = sizeof(buffer); 271 * type = rds_message_next_extension(hdr, &pos, buffer, &buflen); 272 * if (type == RDS_EXTHDR_NONE) 273 * break; 274 * ... 275 * } 276 */ 277 int rds_message_next_extension(struct rds_header *hdr, 278 unsigned int *pos, void *buf, unsigned int *buflen) 279 { 280 unsigned int offset, ext_type, ext_len; 281 u8 *src = hdr->h_exthdr; 282 283 offset = *pos; 284 if (offset >= RDS_HEADER_EXT_SPACE) 285 goto none; 286 287 /* Get the extension type and length. For now, the 288 * length is implied by the extension type. */ 289 ext_type = src[offset++]; 290 291 if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX) 292 goto none; 293 ext_len = rds_exthdr_size[ext_type]; 294 if (offset + ext_len > RDS_HEADER_EXT_SPACE) 295 goto none; 296 297 *pos = offset + ext_len; 298 if (ext_len < *buflen) 299 *buflen = ext_len; 300 memcpy(buf, src + offset, *buflen); 301 return ext_type; 302 303 none: 304 *pos = RDS_HEADER_EXT_SPACE; 305 *buflen = 0; 306 return RDS_EXTHDR_NONE; 307 } 308 309 int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset) 310 { 311 struct rds_ext_header_rdma_dest ext_hdr; 312 313 ext_hdr.h_rdma_rkey = cpu_to_be32(r_key); 314 ext_hdr.h_rdma_offset = cpu_to_be32(offset); 315 return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr); 316 } 317 EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension); 318 319 /* 320 * Each rds_message is allocated with extra space for the scatterlist entries 321 * rds ops will need. This is to minimize memory allocation count. Then, each rds op 322 * can grab SGs when initializing its part of the rds_message. 323 */ 324 struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp) 325 { 326 struct rds_message *rm; 327 328 if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message)) 329 return NULL; 330 331 rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp); 332 if (!rm) 333 goto out; 334 335 rm->m_used_sgs = 0; 336 rm->m_total_sgs = extra_len / sizeof(struct scatterlist); 337 338 refcount_set(&rm->m_refcount, 1); 339 INIT_LIST_HEAD(&rm->m_sock_item); 340 INIT_LIST_HEAD(&rm->m_conn_item); 341 spin_lock_init(&rm->m_rs_lock); 342 init_waitqueue_head(&rm->m_flush_wait); 343 344 out: 345 return rm; 346 } 347 348 /* 349 * RDS ops use this to grab SG entries from the rm's sg pool. 350 */ 351 struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents) 352 { 353 struct scatterlist *sg_first = (struct scatterlist *) &rm[1]; 354 struct scatterlist *sg_ret; 355 356 if (nents <= 0) { 357 pr_warn("rds: alloc sgs failed! nents <= 0\n"); 358 return ERR_PTR(-EINVAL); 359 } 360 361 if (rm->m_used_sgs + nents > rm->m_total_sgs) { 362 pr_warn("rds: alloc sgs failed! total %d used %d nents %d\n", 363 rm->m_total_sgs, rm->m_used_sgs, nents); 364 return ERR_PTR(-ENOMEM); 365 } 366 367 sg_ret = &sg_first[rm->m_used_sgs]; 368 sg_init_table(sg_ret, nents); 369 rm->m_used_sgs += nents; 370 371 return sg_ret; 372 } 373 374 struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len) 375 { 376 struct rds_message *rm; 377 unsigned int i; 378 int num_sgs = DIV_ROUND_UP(total_len, PAGE_SIZE); 379 int extra_bytes = num_sgs * sizeof(struct scatterlist); 380 381 rm = rds_message_alloc(extra_bytes, GFP_NOWAIT); 382 if (!rm) 383 return ERR_PTR(-ENOMEM); 384 385 set_bit(RDS_MSG_PAGEVEC, &rm->m_flags); 386 rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len); 387 rm->data.op_nents = DIV_ROUND_UP(total_len, PAGE_SIZE); 388 rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs); 389 if (IS_ERR(rm->data.op_sg)) { 390 void *err = ERR_CAST(rm->data.op_sg); 391 rds_message_put(rm); 392 return err; 393 } 394 395 for (i = 0; i < rm->data.op_nents; ++i) { 396 sg_set_page(&rm->data.op_sg[i], 397 virt_to_page((void *)page_addrs[i]), 398 PAGE_SIZE, 0); 399 } 400 401 return rm; 402 } 403 404 static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter *from) 405 { 406 struct scatterlist *sg; 407 int ret = 0; 408 int length = iov_iter_count(from); 409 struct rds_msg_zcopy_info *info; 410 411 rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from)); 412 413 /* 414 * now allocate and copy in the data payload. 415 */ 416 sg = rm->data.op_sg; 417 418 info = kzalloc(sizeof(*info), GFP_KERNEL); 419 if (!info) 420 return -ENOMEM; 421 INIT_LIST_HEAD(&info->rs_zcookie_next); 422 rm->data.op_mmp_znotifier = &info->znotif; 423 if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp, 424 length)) { 425 ret = -ENOMEM; 426 goto err; 427 } 428 while (iov_iter_count(from)) { 429 struct page *pages; 430 size_t start; 431 ssize_t copied; 432 433 copied = iov_iter_get_pages2(from, &pages, PAGE_SIZE, 434 1, &start); 435 if (copied < 0) { 436 struct mmpin *mmp; 437 int i; 438 439 for (i = 0; i < rm->data.op_nents; i++) 440 put_page(sg_page(&rm->data.op_sg[i])); 441 mmp = &rm->data.op_mmp_znotifier->z_mmp; 442 mm_unaccount_pinned_pages(mmp); 443 ret = -EFAULT; 444 goto err; 445 } 446 length -= copied; 447 sg_set_page(sg, pages, copied, start); 448 rm->data.op_nents++; 449 sg++; 450 } 451 WARN_ON_ONCE(length != 0); 452 return ret; 453 err: 454 kfree(info); 455 rm->data.op_mmp_znotifier = NULL; 456 return ret; 457 } 458 459 int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from, 460 bool zcopy) 461 { 462 unsigned long to_copy, nbytes; 463 unsigned long sg_off; 464 struct scatterlist *sg; 465 int ret = 0; 466 467 rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from)); 468 469 /* now allocate and copy in the data payload. */ 470 sg = rm->data.op_sg; 471 sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */ 472 473 if (zcopy) 474 return rds_message_zcopy_from_user(rm, from); 475 476 while (iov_iter_count(from)) { 477 if (!sg_page(sg)) { 478 ret = rds_page_remainder_alloc(sg, iov_iter_count(from), 479 GFP_HIGHUSER); 480 if (ret) 481 return ret; 482 rm->data.op_nents++; 483 sg_off = 0; 484 } 485 486 to_copy = min_t(unsigned long, iov_iter_count(from), 487 sg->length - sg_off); 488 489 rds_stats_add(s_copy_from_user, to_copy); 490 nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off, 491 to_copy, from); 492 if (nbytes != to_copy) 493 return -EFAULT; 494 495 sg_off += to_copy; 496 497 if (sg_off == sg->length) 498 sg++; 499 } 500 501 return ret; 502 } 503 504 int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to) 505 { 506 struct rds_message *rm; 507 struct scatterlist *sg; 508 unsigned long to_copy; 509 unsigned long vec_off; 510 int copied; 511 int ret; 512 u32 len; 513 514 rm = container_of(inc, struct rds_message, m_inc); 515 len = be32_to_cpu(rm->m_inc.i_hdr.h_len); 516 517 sg = rm->data.op_sg; 518 vec_off = 0; 519 copied = 0; 520 521 while (iov_iter_count(to) && copied < len) { 522 to_copy = min_t(unsigned long, iov_iter_count(to), 523 sg->length - vec_off); 524 to_copy = min_t(unsigned long, to_copy, len - copied); 525 526 rds_stats_add(s_copy_to_user, to_copy); 527 ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off, 528 to_copy, to); 529 if (ret != to_copy) 530 return -EFAULT; 531 532 vec_off += to_copy; 533 copied += to_copy; 534 535 if (vec_off == sg->length) { 536 vec_off = 0; 537 sg++; 538 } 539 } 540 541 return copied; 542 } 543 544 /* 545 * If the message is still on the send queue, wait until the transport 546 * is done with it. This is particularly important for RDMA operations. 547 */ 548 void rds_message_wait(struct rds_message *rm) 549 { 550 wait_event_interruptible(rm->m_flush_wait, 551 !test_bit(RDS_MSG_MAPPED, &rm->m_flags)); 552 } 553 554 void rds_message_unmapped(struct rds_message *rm) 555 { 556 clear_bit(RDS_MSG_MAPPED, &rm->m_flags); 557 wake_up_interruptible(&rm->m_flush_wait); 558 } 559 EXPORT_SYMBOL_GPL(rds_message_unmapped); 560