1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright(c) 2020 Cornelis Networks, Inc. 4 * Copyright(c) 2015-2018 Intel Corporation. 5 */ 6 #include <asm/page.h> 7 #include <linux/string.h> 8 9 #include "mmu_rb.h" 10 #include "user_exp_rcv.h" 11 #include "trace.h" 12 13 static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt, 14 struct exp_tid_set *set, 15 struct hfi1_filedata *fd); 16 static u32 find_phys_blocks(struct tid_user_buf *tidbuf, unsigned int npages); 17 static int set_rcvarray_entry(struct hfi1_filedata *fd, 18 struct tid_user_buf *tbuf, 19 u32 rcventry, struct tid_group *grp, 20 u16 pageidx, unsigned int npages); 21 static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata, 22 struct tid_rb_node *tnode); 23 static bool tid_rb_invalidate(struct mmu_interval_notifier *mni, 24 const struct mmu_notifier_range *range, 25 unsigned long cur_seq); 26 static bool tid_cover_invalidate(struct mmu_interval_notifier *mni, 27 const struct mmu_notifier_range *range, 28 unsigned long cur_seq); 29 static int program_rcvarray(struct hfi1_filedata *fd, struct tid_user_buf *, 30 struct tid_group *grp, u16 count, 31 u32 *tidlist, unsigned int *tididx, 32 unsigned int *pmapped); 33 static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo); 34 static void __clear_tid_node(struct hfi1_filedata *fd, 35 struct tid_rb_node *node); 36 static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node); 37 38 static const struct mmu_interval_notifier_ops tid_mn_ops = { 39 .invalidate = tid_rb_invalidate, 40 }; 41 static const struct mmu_interval_notifier_ops tid_cover_ops = { 42 .invalidate = tid_cover_invalidate, 43 }; 44 45 /* 46 * Initialize context and file private data needed for Expected 47 * receive caching. This needs to be done after the context has 48 * been configured with the eager/expected RcvEntry counts. 49 */ 50 int hfi1_user_exp_rcv_init(struct hfi1_filedata *fd, 51 struct hfi1_ctxtdata *uctxt) 52 { 53 int ret = 0; 54 55 fd->entry_to_rb = kzalloc_objs(*fd->entry_to_rb, uctxt->expected_count); 56 if (!fd->entry_to_rb) 57 return -ENOMEM; 58 59 if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) { 60 fd->invalid_tid_idx = 0; 61 fd->invalid_tids = kzalloc_objs(*fd->invalid_tids, 62 uctxt->expected_count); 63 if (!fd->invalid_tids) { 64 kfree(fd->entry_to_rb); 65 fd->entry_to_rb = NULL; 66 return -ENOMEM; 67 } 68 fd->use_mn = true; 69 } 70 71 /* 72 * PSM does not have a good way to separate, count, and 73 * effectively enforce a limit on RcvArray entries used by 74 * subctxts (when context sharing is used) when TID caching 75 * is enabled. To help with that, we calculate a per-process 76 * RcvArray entry share and enforce that. 77 * If TID caching is not in use, PSM deals with usage on its 78 * own. In that case, we allow any subctxt to take all of the 79 * entries. 80 * 81 * Make sure that we set the tid counts only after successful 82 * init. 83 */ 84 spin_lock(&fd->tid_lock); 85 if (uctxt->subctxt_cnt && fd->use_mn) { 86 u16 remainder; 87 88 fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt; 89 remainder = uctxt->expected_count % uctxt->subctxt_cnt; 90 if (remainder && fd->subctxt < remainder) 91 fd->tid_limit++; 92 } else { 93 fd->tid_limit = uctxt->expected_count; 94 } 95 spin_unlock(&fd->tid_lock); 96 97 return ret; 98 } 99 100 void hfi1_user_exp_rcv_free(struct hfi1_filedata *fd) 101 { 102 struct hfi1_ctxtdata *uctxt = fd->uctxt; 103 104 mutex_lock(&uctxt->exp_mutex); 105 if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list)) 106 unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd); 107 if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list)) 108 unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd); 109 mutex_unlock(&uctxt->exp_mutex); 110 111 kfree(fd->invalid_tids); 112 fd->invalid_tids = NULL; 113 114 kfree(fd->entry_to_rb); 115 fd->entry_to_rb = NULL; 116 } 117 118 /* 119 * Release pinned receive buffer pages. 120 * 121 * @mapped: true if the pages have been DMA mapped. false otherwise. 122 * @idx: Index of the first page to unpin. 123 * @npages: No of pages to unpin. 124 * 125 * If the pages have been DMA mapped (indicated by mapped parameter), their 126 * info will be passed via a struct tid_rb_node. If they haven't been mapped, 127 * their info will be passed via a struct tid_user_buf. 128 */ 129 static void unpin_rcv_pages(struct hfi1_filedata *fd, 130 struct tid_user_buf *tidbuf, 131 struct tid_rb_node *node, 132 unsigned int idx, 133 unsigned int npages, 134 bool mapped) 135 { 136 struct page **pages; 137 struct hfi1_devdata *dd = fd->uctxt->dd; 138 struct mm_struct *mm; 139 140 if (mapped) { 141 dma_unmap_single(&dd->pcidev->dev, node->dma_addr, 142 node->npages * PAGE_SIZE, DMA_FROM_DEVICE); 143 pages = &node->pages[idx]; 144 mm = mm_from_tid_node(node); 145 } else { 146 pages = &tidbuf->pages[idx]; 147 mm = current->mm; 148 } 149 hfi1_release_user_pages(mm, pages, npages, mapped); 150 fd->tid_n_pinned -= npages; 151 } 152 153 /* 154 * Pin receive buffer pages. 155 */ 156 static int pin_rcv_pages(struct hfi1_filedata *fd, struct tid_user_buf *tidbuf) 157 { 158 int pinned; 159 unsigned int npages = tidbuf->npages; 160 unsigned long vaddr = tidbuf->vaddr; 161 struct page **pages = NULL; 162 struct hfi1_devdata *dd = fd->uctxt->dd; 163 164 if (npages > fd->uctxt->expected_count) { 165 dd_dev_err(dd, "Expected buffer too big\n"); 166 return -EINVAL; 167 } 168 169 /* Allocate the array of struct page pointers needed for pinning */ 170 pages = kzalloc_objs(*pages, npages); 171 if (!pages) 172 return -ENOMEM; 173 174 /* 175 * Pin all the pages of the user buffer. If we can't pin all the 176 * pages, accept the amount pinned so far and program only that. 177 * User space knows how to deal with partially programmed buffers. 178 */ 179 if (!hfi1_can_pin_pages(dd, current->mm, fd->tid_n_pinned, npages)) { 180 kfree(pages); 181 return -ENOMEM; 182 } 183 184 pinned = hfi1_acquire_user_pages(current->mm, vaddr, npages, true, pages); 185 if (pinned <= 0) { 186 kfree(pages); 187 return pinned; 188 } 189 tidbuf->pages = pages; 190 fd->tid_n_pinned += pinned; 191 return pinned; 192 } 193 194 /* 195 * RcvArray entry allocation for Expected Receives is done by the 196 * following algorithm: 197 * 198 * The context keeps 3 lists of groups of RcvArray entries: 199 * 1. List of empty groups - tid_group_list 200 * This list is created during user context creation and 201 * contains elements which describe sets (of 8) of empty 202 * RcvArray entries. 203 * 2. List of partially used groups - tid_used_list 204 * This list contains sets of RcvArray entries which are 205 * not completely used up. Another mapping request could 206 * use some of all of the remaining entries. 207 * 3. List of full groups - tid_full_list 208 * This is the list where sets that are completely used 209 * up go. 210 * 211 * An attempt to optimize the usage of RcvArray entries is 212 * made by finding all sets of physically contiguous pages in a 213 * user's buffer. 214 * These physically contiguous sets are further split into 215 * sizes supported by the receive engine of the HFI. The 216 * resulting sets of pages are stored in struct tid_pageset, 217 * which describes the sets as: 218 * * .count - number of pages in this set 219 * * .idx - starting index into struct page ** array 220 * of this set 221 * 222 * From this point on, the algorithm deals with the page sets 223 * described above. The number of pagesets is divided by the 224 * RcvArray group size to produce the number of full groups 225 * needed. 226 * 227 * Groups from the 3 lists are manipulated using the following 228 * rules: 229 * 1. For each set of 8 pagesets, a complete group from 230 * tid_group_list is taken, programmed, and moved to 231 * the tid_full_list list. 232 * 2. For all remaining pagesets: 233 * 2.1 If the tid_used_list is empty and the tid_group_list 234 * is empty, stop processing pageset and return only 235 * what has been programmed up to this point. 236 * 2.2 If the tid_used_list is empty and the tid_group_list 237 * is not empty, move a group from tid_group_list to 238 * tid_used_list. 239 * 2.3 For each group is tid_used_group, program as much as 240 * can fit into the group. If the group becomes fully 241 * used, move it to tid_full_list. 242 */ 243 int hfi1_user_exp_rcv_setup(struct hfi1_filedata *fd, 244 struct hfi1_tid_info *tinfo) 245 { 246 int ret = 0, need_group = 0, pinned; 247 struct hfi1_ctxtdata *uctxt = fd->uctxt; 248 struct hfi1_devdata *dd = uctxt->dd; 249 unsigned int ngroups, pageset_count, 250 tididx = 0, mapped, mapped_pages = 0; 251 u32 *tidlist = NULL; 252 struct tid_user_buf *tidbuf; 253 unsigned long mmu_seq = 0; 254 255 if (!PAGE_ALIGNED(tinfo->vaddr)) 256 return -EINVAL; 257 if (tinfo->length == 0) 258 return -EINVAL; 259 260 tidbuf = kzalloc_flex(*tidbuf, psets, uctxt->expected_count); 261 if (!tidbuf) 262 return -ENOMEM; 263 264 mutex_init(&tidbuf->cover_mutex); 265 tidbuf->vaddr = tinfo->vaddr; 266 tidbuf->length = tinfo->length; 267 tidbuf->npages = num_user_pages(tidbuf->vaddr, tidbuf->length); 268 269 if (fd->use_mn) { 270 ret = mmu_interval_notifier_insert( 271 &tidbuf->notifier, current->mm, 272 tidbuf->vaddr, tidbuf->npages * PAGE_SIZE, 273 &tid_cover_ops); 274 if (ret) 275 goto fail_release_mem; 276 mmu_seq = mmu_interval_read_begin(&tidbuf->notifier); 277 } 278 279 pinned = pin_rcv_pages(fd, tidbuf); 280 if (pinned <= 0) { 281 ret = (pinned < 0) ? pinned : -ENOSPC; 282 goto fail_unpin; 283 } 284 285 /* Find sets of physically contiguous pages */ 286 tidbuf->n_psets = find_phys_blocks(tidbuf, pinned); 287 288 /* Reserve the number of expected tids to be used. */ 289 spin_lock(&fd->tid_lock); 290 if (fd->tid_used + tidbuf->n_psets > fd->tid_limit) 291 pageset_count = fd->tid_limit - fd->tid_used; 292 else 293 pageset_count = tidbuf->n_psets; 294 fd->tid_used += pageset_count; 295 spin_unlock(&fd->tid_lock); 296 297 if (!pageset_count) { 298 ret = -ENOSPC; 299 goto fail_unreserve; 300 } 301 302 ngroups = pageset_count / dd->rcv_entries.group_size; 303 tidlist = kzalloc_objs(*tidlist, pageset_count); 304 if (!tidlist) { 305 ret = -ENOMEM; 306 goto fail_unreserve; 307 } 308 309 tididx = 0; 310 311 /* 312 * From this point on, we are going to be using shared (between master 313 * and subcontexts) context resources. We need to take the lock. 314 */ 315 mutex_lock(&uctxt->exp_mutex); 316 /* 317 * The first step is to program the RcvArray entries which are complete 318 * groups. 319 */ 320 while (ngroups && uctxt->tid_group_list.count) { 321 struct tid_group *grp = 322 tid_group_pop(&uctxt->tid_group_list); 323 324 ret = program_rcvarray(fd, tidbuf, grp, 325 dd->rcv_entries.group_size, 326 tidlist, &tididx, &mapped); 327 /* 328 * If there was a failure to program the RcvArray 329 * entries for the entire group, reset the grp fields 330 * and add the grp back to the free group list. 331 */ 332 if (ret <= 0) { 333 tid_group_add_tail(grp, &uctxt->tid_group_list); 334 hfi1_cdbg(TID, 335 "Failed to program RcvArray group %d", ret); 336 goto unlock; 337 } 338 339 tid_group_add_tail(grp, &uctxt->tid_full_list); 340 ngroups--; 341 mapped_pages += mapped; 342 } 343 344 while (tididx < pageset_count) { 345 struct tid_group *grp, *ptr; 346 /* 347 * If we don't have any partially used tid groups, check 348 * if we have empty groups. If so, take one from there and 349 * put in the partially used list. 350 */ 351 if (!uctxt->tid_used_list.count || need_group) { 352 if (!uctxt->tid_group_list.count) 353 goto unlock; 354 355 grp = tid_group_pop(&uctxt->tid_group_list); 356 tid_group_add_tail(grp, &uctxt->tid_used_list); 357 need_group = 0; 358 } 359 /* 360 * There is an optimization opportunity here - instead of 361 * fitting as many page sets as we can, check for a group 362 * later on in the list that could fit all of them. 363 */ 364 list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list, 365 list) { 366 unsigned use = min_t(unsigned, pageset_count - tididx, 367 grp->size - grp->used); 368 369 ret = program_rcvarray(fd, tidbuf, grp, 370 use, tidlist, 371 &tididx, &mapped); 372 if (ret < 0) { 373 hfi1_cdbg(TID, 374 "Failed to program RcvArray entries %d", 375 ret); 376 goto unlock; 377 } else if (ret > 0) { 378 if (grp->used == grp->size) 379 tid_group_move(grp, 380 &uctxt->tid_used_list, 381 &uctxt->tid_full_list); 382 mapped_pages += mapped; 383 need_group = 0; 384 /* Check if we are done so we break out early */ 385 if (tididx >= pageset_count) 386 break; 387 } else if (WARN_ON(ret == 0)) { 388 /* 389 * If ret is 0, we did not program any entries 390 * into this group, which can only happen if 391 * we've screwed up the accounting somewhere. 392 * Warn and try to continue. 393 */ 394 need_group = 1; 395 } 396 } 397 } 398 unlock: 399 mutex_unlock(&uctxt->exp_mutex); 400 hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx, 401 mapped_pages, ret); 402 403 /* fail if nothing was programmed, set error if none provided */ 404 if (tididx == 0) { 405 if (ret >= 0) 406 ret = -ENOSPC; 407 goto fail_unreserve; 408 } 409 410 /* adjust reserved tid_used to actual count */ 411 spin_lock(&fd->tid_lock); 412 fd->tid_used -= pageset_count - tididx; 413 spin_unlock(&fd->tid_lock); 414 415 /* unpin all pages not covered by a TID */ 416 unpin_rcv_pages(fd, tidbuf, NULL, mapped_pages, pinned - mapped_pages, 417 false); 418 419 if (fd->use_mn) { 420 /* check for an invalidate during setup */ 421 bool fail = false; 422 423 mutex_lock(&tidbuf->cover_mutex); 424 fail = mmu_interval_read_retry(&tidbuf->notifier, mmu_seq); 425 mutex_unlock(&tidbuf->cover_mutex); 426 427 if (fail) { 428 ret = -EBUSY; 429 goto fail_unprogram; 430 } 431 } 432 433 tinfo->tidcnt = tididx; 434 tinfo->length = mapped_pages * PAGE_SIZE; 435 436 if (copy_to_user(u64_to_user_ptr(tinfo->tidlist), 437 tidlist, sizeof(tidlist[0]) * tididx)) { 438 ret = -EFAULT; 439 goto fail_unprogram; 440 } 441 442 if (fd->use_mn) 443 mmu_interval_notifier_remove(&tidbuf->notifier); 444 kfree(tidbuf->pages); 445 kfree(tidbuf); 446 kfree(tidlist); 447 return 0; 448 449 fail_unprogram: 450 /* unprogram, unmap, and unpin all allocated TIDs */ 451 tinfo->tidlist = (unsigned long)tidlist; 452 hfi1_user_exp_rcv_clear(fd, tinfo); 453 tinfo->tidlist = 0; 454 pinned = 0; /* nothing left to unpin */ 455 pageset_count = 0; /* nothing left reserved */ 456 fail_unreserve: 457 spin_lock(&fd->tid_lock); 458 fd->tid_used -= pageset_count; 459 spin_unlock(&fd->tid_lock); 460 fail_unpin: 461 if (fd->use_mn) 462 mmu_interval_notifier_remove(&tidbuf->notifier); 463 if (pinned > 0) 464 unpin_rcv_pages(fd, tidbuf, NULL, 0, pinned, false); 465 fail_release_mem: 466 kfree(tidbuf->pages); 467 kfree(tidbuf); 468 kfree(tidlist); 469 return ret; 470 } 471 472 int hfi1_user_exp_rcv_clear(struct hfi1_filedata *fd, 473 struct hfi1_tid_info *tinfo) 474 { 475 int ret = 0; 476 struct hfi1_ctxtdata *uctxt = fd->uctxt; 477 u32 *tidinfo; 478 unsigned tididx; 479 480 if (unlikely(tinfo->tidcnt > fd->tid_used)) 481 return -EINVAL; 482 483 tidinfo = memdup_array_user(u64_to_user_ptr(tinfo->tidlist), 484 tinfo->tidcnt, sizeof(tidinfo[0])); 485 if (IS_ERR(tidinfo)) 486 return PTR_ERR(tidinfo); 487 488 mutex_lock(&uctxt->exp_mutex); 489 for (tididx = 0; tididx < tinfo->tidcnt; tididx++) { 490 ret = unprogram_rcvarray(fd, tidinfo[tididx]); 491 if (ret) { 492 hfi1_cdbg(TID, "Failed to unprogram rcv array %d", 493 ret); 494 break; 495 } 496 } 497 spin_lock(&fd->tid_lock); 498 fd->tid_used -= tididx; 499 spin_unlock(&fd->tid_lock); 500 tinfo->tidcnt = tididx; 501 mutex_unlock(&uctxt->exp_mutex); 502 503 kfree(tidinfo); 504 return ret; 505 } 506 507 int hfi1_user_exp_rcv_invalid(struct hfi1_filedata *fd, 508 struct hfi1_tid_info *tinfo) 509 { 510 struct hfi1_ctxtdata *uctxt = fd->uctxt; 511 unsigned long *ev = uctxt->dd->events + 512 (uctxt_offset(uctxt) + fd->subctxt); 513 u32 *array; 514 int ret = 0; 515 516 /* 517 * copy_to_user() can sleep, which will leave the invalid_lock 518 * locked and cause the MMU notifier to be blocked on the lock 519 * for a long time. 520 * Copy the data to a local buffer so we can release the lock. 521 */ 522 array = kzalloc_objs(*array, uctxt->expected_count); 523 if (!array) 524 return -EFAULT; 525 526 spin_lock(&fd->invalid_lock); 527 if (fd->invalid_tid_idx) { 528 memcpy(array, fd->invalid_tids, sizeof(*array) * 529 fd->invalid_tid_idx); 530 memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) * 531 fd->invalid_tid_idx); 532 tinfo->tidcnt = fd->invalid_tid_idx; 533 fd->invalid_tid_idx = 0; 534 /* 535 * Reset the user flag while still holding the lock. 536 * Otherwise, PSM can miss events. 537 */ 538 clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev); 539 } else { 540 tinfo->tidcnt = 0; 541 } 542 spin_unlock(&fd->invalid_lock); 543 544 if (tinfo->tidcnt) { 545 if (copy_to_user((void __user *)tinfo->tidlist, 546 array, sizeof(*array) * tinfo->tidcnt)) 547 ret = -EFAULT; 548 } 549 kfree(array); 550 551 return ret; 552 } 553 554 static u32 find_phys_blocks(struct tid_user_buf *tidbuf, unsigned int npages) 555 { 556 unsigned pagecount, pageidx, setcount = 0, i; 557 unsigned long pfn, this_pfn; 558 struct page **pages = tidbuf->pages; 559 struct tid_pageset *list = tidbuf->psets; 560 561 if (!npages) 562 return 0; 563 564 /* 565 * Look for sets of physically contiguous pages in the user buffer. 566 * This will allow us to optimize Expected RcvArray entry usage by 567 * using the bigger supported sizes. 568 */ 569 pfn = page_to_pfn(pages[0]); 570 for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) { 571 this_pfn = i < npages ? page_to_pfn(pages[i]) : 0; 572 573 /* 574 * If the pfn's are not sequential, pages are not physically 575 * contiguous. 576 */ 577 if (this_pfn != ++pfn) { 578 /* 579 * At this point we have to loop over the set of 580 * physically contiguous pages and break them down it 581 * sizes supported by the HW. 582 * There are two main constraints: 583 * 1. The max buffer size is MAX_EXPECTED_BUFFER. 584 * If the total set size is bigger than that 585 * program only a MAX_EXPECTED_BUFFER chunk. 586 * 2. The buffer size has to be a power of two. If 587 * it is not, round down to the closes power of 588 * 2 and program that size. 589 */ 590 while (pagecount) { 591 int maxpages = pagecount; 592 u32 bufsize = pagecount * PAGE_SIZE; 593 594 if (bufsize > MAX_EXPECTED_BUFFER) 595 maxpages = 596 MAX_EXPECTED_BUFFER >> 597 PAGE_SHIFT; 598 else if (!is_power_of_2(bufsize)) 599 maxpages = 600 rounddown_pow_of_two(bufsize) >> 601 PAGE_SHIFT; 602 603 list[setcount].idx = pageidx; 604 list[setcount].count = maxpages; 605 pagecount -= maxpages; 606 pageidx += maxpages; 607 setcount++; 608 } 609 pageidx = i; 610 pagecount = 1; 611 pfn = this_pfn; 612 } else { 613 pagecount++; 614 } 615 } 616 return setcount; 617 } 618 619 /** 620 * program_rcvarray() - program an RcvArray group with receive buffers 621 * @fd: filedata pointer 622 * @tbuf: pointer to struct tid_user_buf that has the user buffer starting 623 * virtual address, buffer length, page pointers, pagesets (array of 624 * struct tid_pageset holding information on physically contiguous 625 * chunks from the user buffer), and other fields. 626 * @grp: RcvArray group 627 * @count: number of struct tid_pageset's to program 628 * @tidlist: the array of u32 elements when the information about the 629 * programmed RcvArray entries is to be encoded. 630 * @tididx: starting offset into tidlist 631 * @pmapped: (output parameter) number of pages programmed into the RcvArray 632 * entries. 633 * 634 * This function will program up to 'count' number of RcvArray entries from the 635 * group 'grp'. To make best use of write-combining writes, the function will 636 * perform writes to the unused RcvArray entries which will be ignored by the 637 * HW. Each RcvArray entry will be programmed with a physically contiguous 638 * buffer chunk from the user's virtual buffer. 639 * 640 * Return: 641 * -EINVAL if the requested count is larger than the size of the group, 642 * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or 643 * number of RcvArray entries programmed. 644 */ 645 static int program_rcvarray(struct hfi1_filedata *fd, struct tid_user_buf *tbuf, 646 struct tid_group *grp, u16 count, 647 u32 *tidlist, unsigned int *tididx, 648 unsigned int *pmapped) 649 { 650 struct hfi1_ctxtdata *uctxt = fd->uctxt; 651 struct hfi1_devdata *dd = uctxt->dd; 652 u16 idx; 653 unsigned int start = *tididx; 654 u32 tidinfo = 0, rcventry, useidx = 0; 655 int mapped = 0; 656 657 /* Count should never be larger than the group size */ 658 if (count > grp->size) 659 return -EINVAL; 660 661 /* Find the first unused entry in the group */ 662 for (idx = 0; idx < grp->size; idx++) { 663 if (!(grp->map & (1 << idx))) { 664 useidx = idx; 665 break; 666 } 667 rcv_array_wc_fill(dd, grp->base + idx); 668 } 669 670 idx = 0; 671 while (idx < count) { 672 u16 npages, pageidx, setidx = start + idx; 673 int ret = 0; 674 675 /* 676 * If this entry in the group is used, move to the next one. 677 * If we go past the end of the group, exit the loop. 678 */ 679 if (useidx >= grp->size) { 680 break; 681 } else if (grp->map & (1 << useidx)) { 682 rcv_array_wc_fill(dd, grp->base + useidx); 683 useidx++; 684 continue; 685 } 686 687 rcventry = grp->base + useidx; 688 npages = tbuf->psets[setidx].count; 689 pageidx = tbuf->psets[setidx].idx; 690 691 ret = set_rcvarray_entry(fd, tbuf, 692 rcventry, grp, pageidx, 693 npages); 694 if (ret) 695 return ret; 696 mapped += npages; 697 698 tidinfo = create_tid(rcventry - uctxt->expected_base, npages); 699 tidlist[(*tididx)++] = tidinfo; 700 grp->used++; 701 grp->map |= 1 << useidx++; 702 idx++; 703 } 704 705 /* Fill the rest of the group with "blank" writes */ 706 for (; useidx < grp->size; useidx++) 707 rcv_array_wc_fill(dd, grp->base + useidx); 708 *pmapped = mapped; 709 return idx; 710 } 711 712 static int set_rcvarray_entry(struct hfi1_filedata *fd, 713 struct tid_user_buf *tbuf, 714 u32 rcventry, struct tid_group *grp, 715 u16 pageidx, unsigned int npages) 716 { 717 int ret; 718 struct hfi1_ctxtdata *uctxt = fd->uctxt; 719 struct tid_rb_node *node; 720 struct hfi1_devdata *dd = uctxt->dd; 721 dma_addr_t phys; 722 struct page **pages = tbuf->pages + pageidx; 723 724 /* 725 * Allocate the node first so we can handle a potential 726 * failure before we've programmed anything. 727 */ 728 node = kzalloc_flex(*node, pages, npages); 729 if (!node) 730 return -ENOMEM; 731 732 phys = dma_map_single(&dd->pcidev->dev, __va(page_to_phys(pages[0])), 733 npages * PAGE_SIZE, DMA_FROM_DEVICE); 734 if (dma_mapping_error(&dd->pcidev->dev, phys)) { 735 dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n", 736 phys); 737 kfree(node); 738 return -EFAULT; 739 } 740 741 node->fdata = fd; 742 mutex_init(&node->invalidate_mutex); 743 node->phys = page_to_phys(pages[0]); 744 node->npages = npages; 745 node->rcventry = rcventry; 746 node->dma_addr = phys; 747 node->grp = grp; 748 node->freed = false; 749 memcpy(node->pages, pages, flex_array_size(node, pages, npages)); 750 751 if (fd->use_mn) { 752 ret = mmu_interval_notifier_insert( 753 &node->notifier, current->mm, 754 tbuf->vaddr + (pageidx * PAGE_SIZE), npages * PAGE_SIZE, 755 &tid_mn_ops); 756 if (ret) 757 goto out_unmap; 758 } 759 fd->entry_to_rb[node->rcventry - uctxt->expected_base] = node; 760 761 hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1); 762 trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages, 763 node->notifier.interval_tree.start, node->phys, 764 phys); 765 return 0; 766 767 out_unmap: 768 hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d", 769 node->rcventry, node->notifier.interval_tree.start, 770 node->phys, ret); 771 dma_unmap_single(&dd->pcidev->dev, phys, npages * PAGE_SIZE, 772 DMA_FROM_DEVICE); 773 kfree(node); 774 return -EFAULT; 775 } 776 777 static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo) 778 { 779 struct hfi1_ctxtdata *uctxt = fd->uctxt; 780 struct hfi1_devdata *dd = uctxt->dd; 781 struct tid_rb_node *node; 782 u32 tidctrl = EXP_TID_GET(tidinfo, CTRL); 783 u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry; 784 785 if (tidctrl == 0x3 || tidctrl == 0x0) 786 return -EINVAL; 787 788 rcventry = tididx + (tidctrl - 1); 789 790 if (rcventry >= uctxt->expected_count) { 791 dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n", 792 rcventry, uctxt->ctxt); 793 return -EINVAL; 794 } 795 796 node = fd->entry_to_rb[rcventry]; 797 if (!node || node->rcventry != (uctxt->expected_base + rcventry)) 798 return -EBADF; 799 800 if (fd->use_mn) 801 mmu_interval_notifier_remove(&node->notifier); 802 cacheless_tid_rb_remove(fd, node); 803 804 return 0; 805 } 806 807 static void __clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node) 808 { 809 struct hfi1_ctxtdata *uctxt = fd->uctxt; 810 struct hfi1_devdata *dd = uctxt->dd; 811 812 mutex_lock(&node->invalidate_mutex); 813 if (node->freed) 814 goto done; 815 node->freed = true; 816 817 trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry, 818 node->npages, 819 node->notifier.interval_tree.start, node->phys, 820 node->dma_addr); 821 822 /* Make sure device has seen the write before pages are unpinned */ 823 hfi1_put_tid(dd, node->rcventry, PT_INVALID_FLUSH, 0, 0); 824 825 unpin_rcv_pages(fd, NULL, node, 0, node->npages, true); 826 done: 827 mutex_unlock(&node->invalidate_mutex); 828 } 829 830 static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node) 831 { 832 struct hfi1_ctxtdata *uctxt = fd->uctxt; 833 834 __clear_tid_node(fd, node); 835 836 node->grp->used--; 837 node->grp->map &= ~(1 << (node->rcventry - node->grp->base)); 838 839 if (node->grp->used == node->grp->size - 1) 840 tid_group_move(node->grp, &uctxt->tid_full_list, 841 &uctxt->tid_used_list); 842 else if (!node->grp->used) 843 tid_group_move(node->grp, &uctxt->tid_used_list, 844 &uctxt->tid_group_list); 845 kfree(node); 846 } 847 848 /* 849 * As a simple helper for hfi1_user_exp_rcv_free, this function deals with 850 * clearing nodes in the non-cached case. 851 */ 852 static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt, 853 struct exp_tid_set *set, 854 struct hfi1_filedata *fd) 855 { 856 struct tid_group *grp, *ptr; 857 int i; 858 859 list_for_each_entry_safe(grp, ptr, &set->list, list) { 860 list_del_init(&grp->list); 861 862 for (i = 0; i < grp->size; i++) { 863 if (grp->map & (1 << i)) { 864 u16 rcventry = grp->base + i; 865 struct tid_rb_node *node; 866 867 node = fd->entry_to_rb[rcventry - 868 uctxt->expected_base]; 869 if (!node || node->rcventry != rcventry) 870 continue; 871 872 if (fd->use_mn) 873 mmu_interval_notifier_remove( 874 &node->notifier); 875 cacheless_tid_rb_remove(fd, node); 876 } 877 } 878 } 879 } 880 881 static bool tid_rb_invalidate(struct mmu_interval_notifier *mni, 882 const struct mmu_notifier_range *range, 883 unsigned long cur_seq) 884 { 885 struct tid_rb_node *node = 886 container_of(mni, struct tid_rb_node, notifier); 887 struct hfi1_filedata *fdata = node->fdata; 888 struct hfi1_ctxtdata *uctxt = fdata->uctxt; 889 890 if (node->freed) 891 return true; 892 893 /* take action only if unmapping */ 894 if (range->event != MMU_NOTIFY_UNMAP) 895 return true; 896 897 trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, 898 node->notifier.interval_tree.start, 899 node->rcventry, node->npages, node->dma_addr); 900 901 /* clear the hardware rcvarray entry */ 902 __clear_tid_node(fdata, node); 903 904 spin_lock(&fdata->invalid_lock); 905 if (fdata->invalid_tid_idx < uctxt->expected_count) { 906 fdata->invalid_tids[fdata->invalid_tid_idx] = 907 create_tid(node->rcventry - uctxt->expected_base, 908 node->npages); 909 if (!fdata->invalid_tid_idx) { 910 unsigned long *ev; 911 912 /* 913 * hfi1_set_uevent_bits() sets a user event flag 914 * for all processes. Because calling into the 915 * driver to process TID cache invalidations is 916 * expensive and TID cache invalidations are 917 * handled on a per-process basis, we can 918 * optimize this to set the flag only for the 919 * process in question. 920 */ 921 ev = uctxt->dd->events + 922 (uctxt_offset(uctxt) + fdata->subctxt); 923 set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev); 924 } 925 fdata->invalid_tid_idx++; 926 } 927 spin_unlock(&fdata->invalid_lock); 928 return true; 929 } 930 931 static bool tid_cover_invalidate(struct mmu_interval_notifier *mni, 932 const struct mmu_notifier_range *range, 933 unsigned long cur_seq) 934 { 935 struct tid_user_buf *tidbuf = 936 container_of(mni, struct tid_user_buf, notifier); 937 938 /* take action only if unmapping */ 939 if (range->event == MMU_NOTIFY_UNMAP) { 940 mutex_lock(&tidbuf->cover_mutex); 941 mmu_interval_set_seq(mni, cur_seq); 942 mutex_unlock(&tidbuf->cover_mutex); 943 } 944 945 return true; 946 } 947 948 static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata, 949 struct tid_rb_node *tnode) 950 { 951 u32 base = fdata->uctxt->expected_base; 952 953 fdata->entry_to_rb[tnode->rcventry - base] = NULL; 954 clear_tid_node(fdata, tnode); 955 } 956