user_exp_rcv.c 29 KB

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