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 struct rds_znotifier *znotifier; 135 unsigned long i, flags; 136 bool zcopy; 137 138 if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags))) 139 return; 140 141 spin_lock_irqsave(&rm->m_rs_lock, flags); 142 znotifier = rm->data.op_mmp_znotifier; 143 rm->data.op_mmp_znotifier = NULL; 144 zcopy = !!znotifier; 145 146 if (rm->m_rs) { 147 struct rds_sock *rs = rm->m_rs; 148 149 if (znotifier) { 150 rds_rm_zerocopy_callback(rs, znotifier); 151 rds_wake_sk_sleep(rs); 152 } 153 sock_put(rds_rs_to_sk(rs)); 154 rm->m_rs = NULL; 155 } else if (znotifier) { 156 /* 157 * Zerocopy can fail before the message is queued on the 158 * socket, so there is no rs to carry the notification. 159 */ 160 mm_unaccount_pinned_pages(&znotifier->z_mmp); 161 kfree(rds_info_from_znotifier(znotifier)); 162 } 163 spin_unlock_irqrestore(&rm->m_rs_lock, flags); 164 165 for (i = 0; i < rm->data.op_nents; i++) { 166 /* XXX will have to put_page for page refs */ 167 if (!zcopy) 168 __free_page(sg_page(&rm->data.op_sg[i])); 169 else 170 put_page(sg_page(&rm->data.op_sg[i])); 171 } 172 rm->data.op_nents = 0; 173 174 if (rm->rdma.op_active) 175 rds_rdma_free_op(&rm->rdma); 176 if (rm->rdma.op_rdma_mr) 177 kref_put(&rm->rdma.op_rdma_mr->r_kref, __rds_put_mr_final); 178 179 if (rm->atomic.op_active) 180 rds_atomic_free_op(&rm->atomic); 181 if (rm->atomic.op_rdma_mr) 182 kref_put(&rm->atomic.op_rdma_mr->r_kref, __rds_put_mr_final); 183 } 184 185 static void rds_message_unpin_worker(struct work_struct *work) 186 { 187 struct rds_message *rm = container_of(work, struct rds_message, 188 m_unpin_work); 189 190 if (rm->rdma.op_unpin_deferred) 191 rds_rdma_op_unpin_pages(&rm->rdma); 192 if (rm->atomic.op_unpin_deferred) 193 rds_atomic_op_unpin_page(&rm->atomic); 194 195 kfree(rm); 196 } 197 198 void rds_message_put(struct rds_message *rm) 199 { 200 rdsdebug("put rm %p ref %d\n", rm, refcount_read(&rm->m_refcount)); 201 WARN(!refcount_read(&rm->m_refcount), "danger refcount zero on %p\n", rm); 202 if (refcount_dec_and_test(&rm->m_refcount)) { 203 BUG_ON(!list_empty(&rm->m_sock_item)); 204 BUG_ON(!list_empty(&rm->m_conn_item)); 205 206 rds_message_purge(rm); 207 208 /* A final put in atomic context cannot dirty the ops' 209 * user pages on unpin, so rds_rdma_free_op() and 210 * rds_atomic_free_op() deferred it. Finish the unpin, 211 * and the free, from process context. 212 */ 213 if (rm->rdma.op_unpin_deferred || 214 rm->atomic.op_unpin_deferred) { 215 INIT_WORK(&rm->m_unpin_work, rds_message_unpin_worker); 216 queue_work(rds_wq, &rm->m_unpin_work); 217 return; 218 } 219 220 kfree(rm); 221 } 222 } 223 EXPORT_SYMBOL_GPL(rds_message_put); 224 225 void rds_message_populate_header(struct rds_header *hdr, __be16 sport, 226 __be16 dport, u64 seq) 227 { 228 hdr->h_flags = 0; 229 hdr->h_sport = sport; 230 hdr->h_dport = dport; 231 hdr->h_sequence = cpu_to_be64(seq); 232 /* see rds_find_next_ext_space for reason why we memset the 233 * ext header 234 */ 235 memset(hdr->h_exthdr, RDS_EXTHDR_NONE, RDS_HEADER_EXT_SPACE); 236 } 237 EXPORT_SYMBOL_GPL(rds_message_populate_header); 238 239 /* 240 * Find the next place we can add an RDS header extension with 241 * specific length. Extension headers are pushed one after the 242 * other. In the following, the number after the colon is the number 243 * of bytes: 244 * 245 * [ type1:1 dta1:len1 [ type2:1 dta2:len2 ] ... ] RDS_EXTHDR_NONE 246 * 247 * If the extension headers fill the complete extension header space 248 * (16 bytes), the trailing RDS_EXTHDR_NONE is omitted. 249 */ 250 static int rds_find_next_ext_space(struct rds_header *hdr, unsigned int len, 251 u8 **ext_start) 252 { 253 unsigned int ext_len; 254 unsigned int type; 255 int ind = 0; 256 257 while ((ind + 1 + len) <= RDS_HEADER_EXT_SPACE) { 258 if (hdr->h_exthdr[ind] == RDS_EXTHDR_NONE) { 259 *ext_start = hdr->h_exthdr + ind; 260 return 0; 261 } 262 263 type = hdr->h_exthdr[ind]; 264 265 ext_len = (type < __RDS_EXTHDR_MAX) ? rds_exthdr_size[type] : 0; 266 WARN_ONCE(!ext_len, "Unknown ext hdr type %d\n", type); 267 if (!ext_len) 268 return -EINVAL; 269 270 /* ind points to a valid ext hdr with known length */ 271 ind += 1 + ext_len; 272 } 273 274 /* no room for extension */ 275 return -ENOSPC; 276 } 277 278 /* The ext hdr space is prefilled with zero from the kzalloc() */ 279 int rds_message_add_extension(struct rds_header *hdr, 280 unsigned int type, const void *data) 281 { 282 unsigned char *dst; 283 unsigned int len; 284 285 len = (type < __RDS_EXTHDR_MAX) ? rds_exthdr_size[type] : 0; 286 if (!len) 287 return 0; 288 289 if (rds_find_next_ext_space(hdr, len, &dst)) 290 return 0; 291 292 *dst++ = type; 293 memcpy(dst, data, len); 294 295 return 1; 296 } 297 EXPORT_SYMBOL_GPL(rds_message_add_extension); 298 299 /* 300 * If a message has extension headers, retrieve them here. 301 * Call like this: 302 * 303 * unsigned int pos = 0; 304 * 305 * while (1) { 306 * buflen = sizeof(buffer); 307 * type = rds_message_next_extension(hdr, &pos, buffer, &buflen); 308 * if (type == RDS_EXTHDR_NONE) 309 * break; 310 * ... 311 * } 312 */ 313 int rds_message_next_extension(struct rds_header *hdr, 314 unsigned int *pos, void *buf, unsigned int *buflen) 315 { 316 unsigned int offset, ext_type, ext_len; 317 u8 *src = hdr->h_exthdr; 318 319 offset = *pos; 320 if (offset >= RDS_HEADER_EXT_SPACE) 321 goto none; 322 323 /* Get the extension type and length. For now, the 324 * length is implied by the extension type. */ 325 ext_type = src[offset++]; 326 327 if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX) 328 goto none; 329 ext_len = rds_exthdr_size[ext_type]; 330 if (offset + ext_len > RDS_HEADER_EXT_SPACE) 331 goto none; 332 333 *pos = offset + ext_len; 334 if (ext_len < *buflen) 335 *buflen = ext_len; 336 memcpy(buf, src + offset, *buflen); 337 return ext_type; 338 339 none: 340 *pos = RDS_HEADER_EXT_SPACE; 341 *buflen = 0; 342 return RDS_EXTHDR_NONE; 343 } 344 345 int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset) 346 { 347 struct rds_ext_header_rdma_dest ext_hdr; 348 349 ext_hdr.h_rdma_rkey = cpu_to_be32(r_key); 350 ext_hdr.h_rdma_offset = cpu_to_be32(offset); 351 return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr); 352 } 353 EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension); 354 355 /* 356 * Each rds_message is allocated with extra space for the scatterlist entries 357 * rds ops will need. This is to minimize memory allocation count. Then, each rds op 358 * can grab SGs when initializing its part of the rds_message. 359 */ 360 struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp) 361 { 362 struct rds_message *rm; 363 364 if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message)) 365 return NULL; 366 367 rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp); 368 if (!rm) 369 goto out; 370 371 rm->m_used_sgs = 0; 372 rm->m_total_sgs = extra_len / sizeof(struct scatterlist); 373 374 refcount_set(&rm->m_refcount, 1); 375 INIT_LIST_HEAD(&rm->m_sock_item); 376 INIT_LIST_HEAD(&rm->m_conn_item); 377 spin_lock_init(&rm->m_rs_lock); 378 init_waitqueue_head(&rm->m_flush_wait); 379 380 out: 381 return rm; 382 } 383 384 /* 385 * RDS ops use this to grab SG entries from the rm's sg pool. 386 */ 387 struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents) 388 { 389 struct scatterlist *sg_first = (struct scatterlist *) &rm[1]; 390 struct scatterlist *sg_ret; 391 392 if (nents <= 0) { 393 pr_warn("rds: alloc sgs failed! nents <= 0\n"); 394 return ERR_PTR(-EINVAL); 395 } 396 397 if (rm->m_used_sgs + nents > rm->m_total_sgs) { 398 pr_warn("rds: alloc sgs failed! total %d used %d nents %d\n", 399 rm->m_total_sgs, rm->m_used_sgs, nents); 400 return ERR_PTR(-ENOMEM); 401 } 402 403 sg_ret = &sg_first[rm->m_used_sgs]; 404 sg_init_table(sg_ret, nents); 405 rm->m_used_sgs += nents; 406 407 return sg_ret; 408 } 409 410 struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len) 411 { 412 struct rds_message *rm; 413 unsigned int i; 414 int num_sgs = DIV_ROUND_UP(total_len, PAGE_SIZE); 415 int extra_bytes = num_sgs * sizeof(struct scatterlist); 416 417 rm = rds_message_alloc(extra_bytes, GFP_NOWAIT); 418 if (!rm) 419 return ERR_PTR(-ENOMEM); 420 421 set_bit(RDS_MSG_PAGEVEC, &rm->m_flags); 422 rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len); 423 rm->data.op_nents = DIV_ROUND_UP(total_len, PAGE_SIZE); 424 rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs); 425 if (IS_ERR(rm->data.op_sg)) { 426 void *err = ERR_CAST(rm->data.op_sg); 427 rds_message_put(rm); 428 return err; 429 } 430 431 for (i = 0; i < rm->data.op_nents; ++i) { 432 sg_set_page(&rm->data.op_sg[i], 433 virt_to_page((void *)page_addrs[i]), 434 PAGE_SIZE, 0); 435 } 436 437 return rm; 438 } 439 440 static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter *from) 441 { 442 struct scatterlist *sg; 443 int ret = 0; 444 int length = iov_iter_count(from); 445 struct rds_msg_zcopy_info *info; 446 447 rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from)); 448 449 /* 450 * now allocate and copy in the data payload. 451 */ 452 sg = rm->data.op_sg; 453 454 info = kzalloc_obj(*info); 455 if (!info) 456 return -ENOMEM; 457 INIT_LIST_HEAD(&info->rs_zcookie_next); 458 rm->data.op_mmp_znotifier = &info->znotif; 459 if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp, 460 length)) { 461 ret = -ENOMEM; 462 goto err; 463 } 464 while (iov_iter_count(from)) { 465 struct page *pages; 466 size_t start; 467 ssize_t copied; 468 469 copied = iov_iter_get_pages2(from, &pages, PAGE_SIZE, 470 1, &start); 471 if (copied < 0) { 472 struct mmpin *mmp; 473 int i; 474 475 for (i = 0; i < rm->data.op_nents; i++) 476 put_page(sg_page(&rm->data.op_sg[i])); 477 rm->data.op_nents = 0; 478 mmp = &rm->data.op_mmp_znotifier->z_mmp; 479 mm_unaccount_pinned_pages(mmp); 480 ret = -EFAULT; 481 goto err; 482 } 483 length -= copied; 484 sg_set_page(sg, pages, copied, start); 485 rm->data.op_nents++; 486 sg++; 487 } 488 WARN_ON_ONCE(length != 0); 489 return ret; 490 err: 491 kfree(info); 492 rm->data.op_mmp_znotifier = NULL; 493 return ret; 494 } 495 496 int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from, 497 bool zcopy) 498 { 499 unsigned long to_copy, nbytes; 500 unsigned long sg_off; 501 struct scatterlist *sg; 502 int ret = 0; 503 504 rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from)); 505 506 /* now allocate and copy in the data payload. */ 507 sg = rm->data.op_sg; 508 sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */ 509 510 if (zcopy) 511 return rds_message_zcopy_from_user(rm, from); 512 513 while (iov_iter_count(from)) { 514 if (!sg_page(sg)) { 515 ret = rds_page_remainder_alloc(sg, iov_iter_count(from), 516 GFP_HIGHUSER); 517 if (ret) 518 return ret; 519 rm->data.op_nents++; 520 sg_off = 0; 521 } 522 523 to_copy = min_t(unsigned long, iov_iter_count(from), 524 sg->length - sg_off); 525 526 rds_stats_add(s_copy_from_user, to_copy); 527 nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off, 528 to_copy, from); 529 if (nbytes != to_copy) 530 return -EFAULT; 531 532 sg_off += to_copy; 533 534 if (sg_off == sg->length) 535 sg++; 536 } 537 538 return ret; 539 } 540 541 int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to) 542 { 543 struct rds_message *rm; 544 struct scatterlist *sg; 545 unsigned long to_copy; 546 unsigned long vec_off; 547 int copied; 548 int ret; 549 u32 len; 550 551 rm = container_of(inc, struct rds_message, m_inc); 552 len = be32_to_cpu(rm->m_inc.i_hdr.h_len); 553 554 sg = rm->data.op_sg; 555 vec_off = 0; 556 copied = 0; 557 558 while (iov_iter_count(to) && copied < len) { 559 to_copy = min_t(unsigned long, iov_iter_count(to), 560 sg->length - vec_off); 561 to_copy = min_t(unsigned long, to_copy, len - copied); 562 563 rds_stats_add(s_copy_to_user, to_copy); 564 ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off, 565 to_copy, to); 566 if (ret != to_copy) 567 return -EFAULT; 568 569 vec_off += to_copy; 570 copied += to_copy; 571 572 if (vec_off == sg->length) { 573 vec_off = 0; 574 sg++; 575 } 576 } 577 578 return copied; 579 } 580 581 /* 582 * If the message is still on the send queue, wait until the transport 583 * is done with it. This is particularly important for RDMA operations. 584 */ 585 void rds_message_wait(struct rds_message *rm) 586 { 587 wait_event_interruptible(rm->m_flush_wait, 588 !test_bit(RDS_MSG_MAPPED, &rm->m_flags)); 589 } 590 591 void rds_message_unmapped(struct rds_message *rm) 592 { 593 clear_bit(RDS_MSG_MAPPED, &rm->m_flags); 594 wake_up_interruptible(&rm->m_flush_wait); 595 } 596 EXPORT_SYMBOL_GPL(rds_message_unmapped); 597