user_exp_rcv.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * Copyright(c) 2015-2017 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. spin_lock_init(&fd->tid_lock);
  89. spin_lock_init(&fd->invalid_lock);
  90. fd->entry_to_rb = kcalloc(uctxt->expected_count,
  91. sizeof(struct rb_node *),
  92. GFP_KERNEL);
  93. if (!fd->entry_to_rb)
  94. return -ENOMEM;
  95. if (!HFI1_CAP_UGET_MASK(uctxt->flags, TID_UNMAP)) {
  96. fd->invalid_tid_idx = 0;
  97. fd->invalid_tids = kcalloc(uctxt->expected_count,
  98. sizeof(*fd->invalid_tids),
  99. GFP_KERNEL);
  100. if (!fd->invalid_tids) {
  101. kfree(fd->entry_to_rb);
  102. fd->entry_to_rb = NULL;
  103. return -ENOMEM;
  104. }
  105. /*
  106. * Register MMU notifier callbacks. If the registration
  107. * fails, continue without TID caching for this context.
  108. */
  109. ret = hfi1_mmu_rb_register(fd, fd->mm, &tid_rb_ops,
  110. dd->pport->hfi1_wq,
  111. &fd->handler);
  112. if (ret) {
  113. dd_dev_info(dd,
  114. "Failed MMU notifier registration %d\n",
  115. ret);
  116. ret = 0;
  117. }
  118. }
  119. /*
  120. * PSM does not have a good way to separate, count, and
  121. * effectively enforce a limit on RcvArray entries used by
  122. * subctxts (when context sharing is used) when TID caching
  123. * is enabled. To help with that, we calculate a per-process
  124. * RcvArray entry share and enforce that.
  125. * If TID caching is not in use, PSM deals with usage on its
  126. * own. In that case, we allow any subctxt to take all of the
  127. * entries.
  128. *
  129. * Make sure that we set the tid counts only after successful
  130. * init.
  131. */
  132. spin_lock(&fd->tid_lock);
  133. if (uctxt->subctxt_cnt && fd->handler) {
  134. u16 remainder;
  135. fd->tid_limit = uctxt->expected_count / uctxt->subctxt_cnt;
  136. remainder = uctxt->expected_count % uctxt->subctxt_cnt;
  137. if (remainder && fd->subctxt < remainder)
  138. fd->tid_limit++;
  139. } else {
  140. fd->tid_limit = uctxt->expected_count;
  141. }
  142. spin_unlock(&fd->tid_lock);
  143. return ret;
  144. }
  145. void hfi1_user_exp_rcv_free(struct hfi1_filedata *fd)
  146. {
  147. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  148. /*
  149. * The notifier would have been removed when the process'es mm
  150. * was freed.
  151. */
  152. if (fd->handler) {
  153. hfi1_mmu_rb_unregister(fd->handler);
  154. } else {
  155. if (!EXP_TID_SET_EMPTY(uctxt->tid_full_list))
  156. unlock_exp_tids(uctxt, &uctxt->tid_full_list, fd);
  157. if (!EXP_TID_SET_EMPTY(uctxt->tid_used_list))
  158. unlock_exp_tids(uctxt, &uctxt->tid_used_list, fd);
  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. tidbuf = kzalloc(sizeof(*tidbuf), GFP_KERNEL);
  303. if (!tidbuf)
  304. return -ENOMEM;
  305. tidbuf->vaddr = tinfo->vaddr;
  306. tidbuf->length = tinfo->length;
  307. tidbuf->psets = kcalloc(uctxt->expected_count, sizeof(*tidbuf->psets),
  308. GFP_KERNEL);
  309. if (!tidbuf->psets) {
  310. kfree(tidbuf);
  311. return -ENOMEM;
  312. }
  313. pinned = pin_rcv_pages(fd, tidbuf);
  314. if (pinned <= 0) {
  315. kfree(tidbuf->psets);
  316. kfree(tidbuf);
  317. return pinned;
  318. }
  319. /* Find sets of physically contiguous pages */
  320. tidbuf->n_psets = find_phys_blocks(tidbuf, pinned);
  321. /*
  322. * We don't need to access this under a lock since tid_used is per
  323. * process and the same process cannot be in hfi1_user_exp_rcv_clear()
  324. * and hfi1_user_exp_rcv_setup() at the same time.
  325. */
  326. spin_lock(&fd->tid_lock);
  327. if (fd->tid_used + tidbuf->n_psets > fd->tid_limit)
  328. pageset_count = fd->tid_limit - fd->tid_used;
  329. else
  330. pageset_count = tidbuf->n_psets;
  331. spin_unlock(&fd->tid_lock);
  332. if (!pageset_count)
  333. goto bail;
  334. ngroups = pageset_count / dd->rcv_entries.group_size;
  335. tidlist = kcalloc(pageset_count, sizeof(*tidlist), GFP_KERNEL);
  336. if (!tidlist) {
  337. ret = -ENOMEM;
  338. goto nomem;
  339. }
  340. tididx = 0;
  341. /*
  342. * From this point on, we are going to be using shared (between master
  343. * and subcontexts) context resources. We need to take the lock.
  344. */
  345. mutex_lock(&uctxt->exp_lock);
  346. /*
  347. * The first step is to program the RcvArray entries which are complete
  348. * groups.
  349. */
  350. while (ngroups && uctxt->tid_group_list.count) {
  351. struct tid_group *grp =
  352. tid_group_pop(&uctxt->tid_group_list);
  353. ret = program_rcvarray(fd, tidbuf, grp,
  354. pageidx, dd->rcv_entries.group_size,
  355. tidlist, &tididx, &mapped);
  356. /*
  357. * If there was a failure to program the RcvArray
  358. * entries for the entire group, reset the grp fields
  359. * and add the grp back to the free group list.
  360. */
  361. if (ret <= 0) {
  362. tid_group_add_tail(grp, &uctxt->tid_group_list);
  363. hfi1_cdbg(TID,
  364. "Failed to program RcvArray group %d", ret);
  365. goto unlock;
  366. }
  367. tid_group_add_tail(grp, &uctxt->tid_full_list);
  368. ngroups--;
  369. pageidx += ret;
  370. mapped_pages += mapped;
  371. }
  372. while (pageidx < pageset_count) {
  373. struct tid_group *grp, *ptr;
  374. /*
  375. * If we don't have any partially used tid groups, check
  376. * if we have empty groups. If so, take one from there and
  377. * put in the partially used list.
  378. */
  379. if (!uctxt->tid_used_list.count || need_group) {
  380. if (!uctxt->tid_group_list.count)
  381. goto unlock;
  382. grp = tid_group_pop(&uctxt->tid_group_list);
  383. tid_group_add_tail(grp, &uctxt->tid_used_list);
  384. need_group = 0;
  385. }
  386. /*
  387. * There is an optimization opportunity here - instead of
  388. * fitting as many page sets as we can, check for a group
  389. * later on in the list that could fit all of them.
  390. */
  391. list_for_each_entry_safe(grp, ptr, &uctxt->tid_used_list.list,
  392. list) {
  393. unsigned use = min_t(unsigned, pageset_count - pageidx,
  394. grp->size - grp->used);
  395. ret = program_rcvarray(fd, tidbuf, grp,
  396. pageidx, use, tidlist,
  397. &tididx, &mapped);
  398. if (ret < 0) {
  399. hfi1_cdbg(TID,
  400. "Failed to program RcvArray entries %d",
  401. ret);
  402. ret = -EFAULT;
  403. goto unlock;
  404. } else if (ret > 0) {
  405. if (grp->used == grp->size)
  406. tid_group_move(grp,
  407. &uctxt->tid_used_list,
  408. &uctxt->tid_full_list);
  409. pageidx += ret;
  410. mapped_pages += mapped;
  411. need_group = 0;
  412. /* Check if we are done so we break out early */
  413. if (pageidx >= pageset_count)
  414. break;
  415. } else if (WARN_ON(ret == 0)) {
  416. /*
  417. * If ret is 0, we did not program any entries
  418. * into this group, which can only happen if
  419. * we've screwed up the accounting somewhere.
  420. * Warn and try to continue.
  421. */
  422. need_group = 1;
  423. }
  424. }
  425. }
  426. unlock:
  427. mutex_unlock(&uctxt->exp_lock);
  428. nomem:
  429. hfi1_cdbg(TID, "total mapped: tidpairs:%u pages:%u (%d)", tididx,
  430. mapped_pages, ret);
  431. if (tididx) {
  432. spin_lock(&fd->tid_lock);
  433. fd->tid_used += tididx;
  434. spin_unlock(&fd->tid_lock);
  435. tinfo->tidcnt = tididx;
  436. tinfo->length = mapped_pages * PAGE_SIZE;
  437. if (copy_to_user(u64_to_user_ptr(tinfo->tidlist),
  438. tidlist, sizeof(tidlist[0]) * tididx)) {
  439. /*
  440. * On failure to copy to the user level, we need to undo
  441. * everything done so far so we don't leak resources.
  442. */
  443. tinfo->tidlist = (unsigned long)&tidlist;
  444. hfi1_user_exp_rcv_clear(fd, tinfo);
  445. tinfo->tidlist = 0;
  446. ret = -EFAULT;
  447. goto bail;
  448. }
  449. }
  450. /*
  451. * If not everything was mapped (due to insufficient RcvArray entries,
  452. * for example), unpin all unmapped pages so we can pin them nex time.
  453. */
  454. if (mapped_pages != pinned)
  455. unpin_rcv_pages(fd, tidbuf, NULL, mapped_pages,
  456. (pinned - mapped_pages), false);
  457. bail:
  458. kfree(tidbuf->psets);
  459. kfree(tidlist);
  460. kfree(tidbuf->pages);
  461. kfree(tidbuf);
  462. return ret > 0 ? 0 : ret;
  463. }
  464. int hfi1_user_exp_rcv_clear(struct hfi1_filedata *fd,
  465. struct hfi1_tid_info *tinfo)
  466. {
  467. int ret = 0;
  468. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  469. u32 *tidinfo;
  470. unsigned tididx;
  471. if (unlikely(tinfo->tidcnt > fd->tid_used))
  472. return -EINVAL;
  473. tidinfo = memdup_user(u64_to_user_ptr(tinfo->tidlist),
  474. sizeof(tidinfo[0]) * tinfo->tidcnt);
  475. if (IS_ERR(tidinfo))
  476. return PTR_ERR(tidinfo);
  477. mutex_lock(&uctxt->exp_lock);
  478. for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
  479. ret = unprogram_rcvarray(fd, tidinfo[tididx], NULL);
  480. if (ret) {
  481. hfi1_cdbg(TID, "Failed to unprogram rcv array %d",
  482. ret);
  483. break;
  484. }
  485. }
  486. spin_lock(&fd->tid_lock);
  487. fd->tid_used -= tididx;
  488. spin_unlock(&fd->tid_lock);
  489. tinfo->tidcnt = tididx;
  490. mutex_unlock(&uctxt->exp_lock);
  491. kfree(tidinfo);
  492. return ret;
  493. }
  494. int hfi1_user_exp_rcv_invalid(struct hfi1_filedata *fd,
  495. struct hfi1_tid_info *tinfo)
  496. {
  497. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  498. unsigned long *ev = uctxt->dd->events +
  499. (uctxt_offset(uctxt) + fd->subctxt);
  500. u32 *array;
  501. int ret = 0;
  502. /*
  503. * copy_to_user() can sleep, which will leave the invalid_lock
  504. * locked and cause the MMU notifier to be blocked on the lock
  505. * for a long time.
  506. * Copy the data to a local buffer so we can release the lock.
  507. */
  508. array = kcalloc(uctxt->expected_count, sizeof(*array), GFP_KERNEL);
  509. if (!array)
  510. return -EFAULT;
  511. spin_lock(&fd->invalid_lock);
  512. if (fd->invalid_tid_idx) {
  513. memcpy(array, fd->invalid_tids, sizeof(*array) *
  514. fd->invalid_tid_idx);
  515. memset(fd->invalid_tids, 0, sizeof(*fd->invalid_tids) *
  516. fd->invalid_tid_idx);
  517. tinfo->tidcnt = fd->invalid_tid_idx;
  518. fd->invalid_tid_idx = 0;
  519. /*
  520. * Reset the user flag while still holding the lock.
  521. * Otherwise, PSM can miss events.
  522. */
  523. clear_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
  524. } else {
  525. tinfo->tidcnt = 0;
  526. }
  527. spin_unlock(&fd->invalid_lock);
  528. if (tinfo->tidcnt) {
  529. if (copy_to_user((void __user *)tinfo->tidlist,
  530. array, sizeof(*array) * tinfo->tidcnt))
  531. ret = -EFAULT;
  532. }
  533. kfree(array);
  534. return ret;
  535. }
  536. static u32 find_phys_blocks(struct tid_user_buf *tidbuf, unsigned int npages)
  537. {
  538. unsigned pagecount, pageidx, setcount = 0, i;
  539. unsigned long pfn, this_pfn;
  540. struct page **pages = tidbuf->pages;
  541. struct tid_pageset *list = tidbuf->psets;
  542. if (!npages)
  543. return 0;
  544. /*
  545. * Look for sets of physically contiguous pages in the user buffer.
  546. * This will allow us to optimize Expected RcvArray entry usage by
  547. * using the bigger supported sizes.
  548. */
  549. pfn = page_to_pfn(pages[0]);
  550. for (pageidx = 0, pagecount = 1, i = 1; i <= npages; i++) {
  551. this_pfn = i < npages ? page_to_pfn(pages[i]) : 0;
  552. /*
  553. * If the pfn's are not sequential, pages are not physically
  554. * contiguous.
  555. */
  556. if (this_pfn != ++pfn) {
  557. /*
  558. * At this point we have to loop over the set of
  559. * physically contiguous pages and break them down it
  560. * sizes supported by the HW.
  561. * There are two main constraints:
  562. * 1. The max buffer size is MAX_EXPECTED_BUFFER.
  563. * If the total set size is bigger than that
  564. * program only a MAX_EXPECTED_BUFFER chunk.
  565. * 2. The buffer size has to be a power of two. If
  566. * it is not, round down to the closes power of
  567. * 2 and program that size.
  568. */
  569. while (pagecount) {
  570. int maxpages = pagecount;
  571. u32 bufsize = pagecount * PAGE_SIZE;
  572. if (bufsize > MAX_EXPECTED_BUFFER)
  573. maxpages =
  574. MAX_EXPECTED_BUFFER >>
  575. PAGE_SHIFT;
  576. else if (!is_power_of_2(bufsize))
  577. maxpages =
  578. rounddown_pow_of_two(bufsize) >>
  579. PAGE_SHIFT;
  580. list[setcount].idx = pageidx;
  581. list[setcount].count = maxpages;
  582. pagecount -= maxpages;
  583. pageidx += maxpages;
  584. setcount++;
  585. }
  586. pageidx = i;
  587. pagecount = 1;
  588. pfn = this_pfn;
  589. } else {
  590. pagecount++;
  591. }
  592. }
  593. return setcount;
  594. }
  595. /**
  596. * program_rcvarray() - program an RcvArray group with receive buffers
  597. * @fd: filedata pointer
  598. * @tbuf: pointer to struct tid_user_buf that has the user buffer starting
  599. * virtual address, buffer length, page pointers, pagesets (array of
  600. * struct tid_pageset holding information on physically contiguous
  601. * chunks from the user buffer), and other fields.
  602. * @grp: RcvArray group
  603. * @start: starting index into sets array
  604. * @count: number of struct tid_pageset's to program
  605. * @tidlist: the array of u32 elements when the information about the
  606. * programmed RcvArray entries is to be encoded.
  607. * @tididx: starting offset into tidlist
  608. * @pmapped: (output parameter) number of pages programmed into the RcvArray
  609. * entries.
  610. *
  611. * This function will program up to 'count' number of RcvArray entries from the
  612. * group 'grp'. To make best use of write-combining writes, the function will
  613. * perform writes to the unused RcvArray entries which will be ignored by the
  614. * HW. Each RcvArray entry will be programmed with a physically contiguous
  615. * buffer chunk from the user's virtual buffer.
  616. *
  617. * Return:
  618. * -EINVAL if the requested count is larger than the size of the group,
  619. * -ENOMEM or -EFAULT on error from set_rcvarray_entry(), or
  620. * number of RcvArray entries programmed.
  621. */
  622. static int program_rcvarray(struct hfi1_filedata *fd, struct tid_user_buf *tbuf,
  623. struct tid_group *grp,
  624. unsigned int start, u16 count,
  625. u32 *tidlist, unsigned int *tididx,
  626. unsigned int *pmapped)
  627. {
  628. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  629. struct hfi1_devdata *dd = uctxt->dd;
  630. u16 idx;
  631. u32 tidinfo = 0, rcventry, useidx = 0;
  632. int mapped = 0;
  633. /* Count should never be larger than the group size */
  634. if (count > grp->size)
  635. return -EINVAL;
  636. /* Find the first unused entry in the group */
  637. for (idx = 0; idx < grp->size; idx++) {
  638. if (!(grp->map & (1 << idx))) {
  639. useidx = idx;
  640. break;
  641. }
  642. rcv_array_wc_fill(dd, grp->base + idx);
  643. }
  644. idx = 0;
  645. while (idx < count) {
  646. u16 npages, pageidx, setidx = start + idx;
  647. int ret = 0;
  648. /*
  649. * If this entry in the group is used, move to the next one.
  650. * If we go past the end of the group, exit the loop.
  651. */
  652. if (useidx >= grp->size) {
  653. break;
  654. } else if (grp->map & (1 << useidx)) {
  655. rcv_array_wc_fill(dd, grp->base + useidx);
  656. useidx++;
  657. continue;
  658. }
  659. rcventry = grp->base + useidx;
  660. npages = tbuf->psets[setidx].count;
  661. pageidx = tbuf->psets[setidx].idx;
  662. ret = set_rcvarray_entry(fd, tbuf,
  663. rcventry, grp, pageidx,
  664. npages);
  665. if (ret)
  666. return ret;
  667. mapped += npages;
  668. tidinfo = rcventry2tidinfo(rcventry - uctxt->expected_base) |
  669. EXP_TID_SET(LEN, npages);
  670. tidlist[(*tididx)++] = tidinfo;
  671. grp->used++;
  672. grp->map |= 1 << useidx++;
  673. idx++;
  674. }
  675. /* Fill the rest of the group with "blank" writes */
  676. for (; useidx < grp->size; useidx++)
  677. rcv_array_wc_fill(dd, grp->base + useidx);
  678. *pmapped = mapped;
  679. return idx;
  680. }
  681. static int set_rcvarray_entry(struct hfi1_filedata *fd,
  682. struct tid_user_buf *tbuf,
  683. u32 rcventry, struct tid_group *grp,
  684. u16 pageidx, unsigned int npages)
  685. {
  686. int ret;
  687. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  688. struct tid_rb_node *node;
  689. struct hfi1_devdata *dd = uctxt->dd;
  690. dma_addr_t phys;
  691. struct page **pages = tbuf->pages + pageidx;
  692. /*
  693. * Allocate the node first so we can handle a potential
  694. * failure before we've programmed anything.
  695. */
  696. node = kzalloc(sizeof(*node) + (sizeof(struct page *) * npages),
  697. GFP_KERNEL);
  698. if (!node)
  699. return -ENOMEM;
  700. phys = pci_map_single(dd->pcidev,
  701. __va(page_to_phys(pages[0])),
  702. npages * PAGE_SIZE, PCI_DMA_FROMDEVICE);
  703. if (dma_mapping_error(&dd->pcidev->dev, phys)) {
  704. dd_dev_err(dd, "Failed to DMA map Exp Rcv pages 0x%llx\n",
  705. phys);
  706. kfree(node);
  707. return -EFAULT;
  708. }
  709. node->mmu.addr = tbuf->vaddr + (pageidx * PAGE_SIZE);
  710. node->mmu.len = npages * PAGE_SIZE;
  711. node->phys = page_to_phys(pages[0]);
  712. node->npages = npages;
  713. node->rcventry = rcventry;
  714. node->dma_addr = phys;
  715. node->grp = grp;
  716. node->freed = false;
  717. memcpy(node->pages, pages, sizeof(struct page *) * npages);
  718. if (!fd->handler)
  719. ret = tid_rb_insert(fd, &node->mmu);
  720. else
  721. ret = hfi1_mmu_rb_insert(fd->handler, &node->mmu);
  722. if (ret) {
  723. hfi1_cdbg(TID, "Failed to insert RB node %u 0x%lx, 0x%lx %d",
  724. node->rcventry, node->mmu.addr, node->phys, ret);
  725. pci_unmap_single(dd->pcidev, phys, npages * PAGE_SIZE,
  726. PCI_DMA_FROMDEVICE);
  727. kfree(node);
  728. return -EFAULT;
  729. }
  730. hfi1_put_tid(dd, rcventry, PT_EXPECTED, phys, ilog2(npages) + 1);
  731. trace_hfi1_exp_tid_reg(uctxt->ctxt, fd->subctxt, rcventry, npages,
  732. node->mmu.addr, node->phys, phys);
  733. return 0;
  734. }
  735. static int unprogram_rcvarray(struct hfi1_filedata *fd, u32 tidinfo,
  736. struct tid_group **grp)
  737. {
  738. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  739. struct hfi1_devdata *dd = uctxt->dd;
  740. struct tid_rb_node *node;
  741. u8 tidctrl = EXP_TID_GET(tidinfo, CTRL);
  742. u32 tididx = EXP_TID_GET(tidinfo, IDX) << 1, rcventry;
  743. if (tididx >= uctxt->expected_count) {
  744. dd_dev_err(dd, "Invalid RcvArray entry (%u) index for ctxt %u\n",
  745. tididx, uctxt->ctxt);
  746. return -EINVAL;
  747. }
  748. if (tidctrl == 0x3)
  749. return -EINVAL;
  750. rcventry = tididx + (tidctrl - 1);
  751. node = fd->entry_to_rb[rcventry];
  752. if (!node || node->rcventry != (uctxt->expected_base + rcventry))
  753. return -EBADF;
  754. if (grp)
  755. *grp = node->grp;
  756. if (!fd->handler)
  757. cacheless_tid_rb_remove(fd, node);
  758. else
  759. hfi1_mmu_rb_remove(fd->handler, &node->mmu);
  760. return 0;
  761. }
  762. static void clear_tid_node(struct hfi1_filedata *fd, struct tid_rb_node *node)
  763. {
  764. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  765. struct hfi1_devdata *dd = uctxt->dd;
  766. trace_hfi1_exp_tid_unreg(uctxt->ctxt, fd->subctxt, node->rcventry,
  767. node->npages, node->mmu.addr, node->phys,
  768. node->dma_addr);
  769. /*
  770. * Make sure device has seen the write before we unpin the
  771. * pages.
  772. */
  773. hfi1_put_tid(dd, node->rcventry, PT_INVALID_FLUSH, 0, 0);
  774. unpin_rcv_pages(fd, NULL, node, 0, node->npages, true);
  775. node->grp->used--;
  776. node->grp->map &= ~(1 << (node->rcventry - node->grp->base));
  777. if (node->grp->used == node->grp->size - 1)
  778. tid_group_move(node->grp, &uctxt->tid_full_list,
  779. &uctxt->tid_used_list);
  780. else if (!node->grp->used)
  781. tid_group_move(node->grp, &uctxt->tid_used_list,
  782. &uctxt->tid_group_list);
  783. kfree(node);
  784. }
  785. /*
  786. * As a simple helper for hfi1_user_exp_rcv_free, this function deals with
  787. * clearing nodes in the non-cached case.
  788. */
  789. static void unlock_exp_tids(struct hfi1_ctxtdata *uctxt,
  790. struct exp_tid_set *set,
  791. struct hfi1_filedata *fd)
  792. {
  793. struct tid_group *grp, *ptr;
  794. int i;
  795. list_for_each_entry_safe(grp, ptr, &set->list, list) {
  796. list_del_init(&grp->list);
  797. for (i = 0; i < grp->size; i++) {
  798. if (grp->map & (1 << i)) {
  799. u16 rcventry = grp->base + i;
  800. struct tid_rb_node *node;
  801. node = fd->entry_to_rb[rcventry -
  802. uctxt->expected_base];
  803. if (!node || node->rcventry != rcventry)
  804. continue;
  805. cacheless_tid_rb_remove(fd, node);
  806. }
  807. }
  808. }
  809. }
  810. /*
  811. * Always return 0 from this function. A non-zero return indicates that the
  812. * remove operation will be called and that memory should be unpinned.
  813. * However, the driver cannot unpin out from under PSM. Instead, retain the
  814. * memory (by returning 0) and inform PSM that the memory is going away. PSM
  815. * will call back later when it has removed the memory from its list.
  816. */
  817. static int tid_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
  818. {
  819. struct hfi1_filedata *fdata = arg;
  820. struct hfi1_ctxtdata *uctxt = fdata->uctxt;
  821. struct tid_rb_node *node =
  822. container_of(mnode, struct tid_rb_node, mmu);
  823. if (node->freed)
  824. return 0;
  825. trace_hfi1_exp_tid_inval(uctxt->ctxt, fdata->subctxt, node->mmu.addr,
  826. node->rcventry, node->npages, node->dma_addr);
  827. node->freed = true;
  828. spin_lock(&fdata->invalid_lock);
  829. if (fdata->invalid_tid_idx < uctxt->expected_count) {
  830. fdata->invalid_tids[fdata->invalid_tid_idx] =
  831. rcventry2tidinfo(node->rcventry - uctxt->expected_base);
  832. fdata->invalid_tids[fdata->invalid_tid_idx] |=
  833. EXP_TID_SET(LEN, node->npages);
  834. if (!fdata->invalid_tid_idx) {
  835. unsigned long *ev;
  836. /*
  837. * hfi1_set_uevent_bits() sets a user event flag
  838. * for all processes. Because calling into the
  839. * driver to process TID cache invalidations is
  840. * expensive and TID cache invalidations are
  841. * handled on a per-process basis, we can
  842. * optimize this to set the flag only for the
  843. * process in question.
  844. */
  845. ev = uctxt->dd->events +
  846. (uctxt_offset(uctxt) + fdata->subctxt);
  847. set_bit(_HFI1_EVENT_TID_MMU_NOTIFY_BIT, ev);
  848. }
  849. fdata->invalid_tid_idx++;
  850. }
  851. spin_unlock(&fdata->invalid_lock);
  852. return 0;
  853. }
  854. static int tid_rb_insert(void *arg, struct mmu_rb_node *node)
  855. {
  856. struct hfi1_filedata *fdata = arg;
  857. struct tid_rb_node *tnode =
  858. container_of(node, struct tid_rb_node, mmu);
  859. u32 base = fdata->uctxt->expected_base;
  860. fdata->entry_to_rb[tnode->rcventry - base] = tnode;
  861. return 0;
  862. }
  863. static void cacheless_tid_rb_remove(struct hfi1_filedata *fdata,
  864. struct tid_rb_node *tnode)
  865. {
  866. u32 base = fdata->uctxt->expected_base;
  867. fdata->entry_to_rb[tnode->rcventry - base] = NULL;
  868. clear_tid_node(fdata, tnode);
  869. }
  870. static void tid_rb_remove(void *arg, struct mmu_rb_node *node)
  871. {
  872. struct hfi1_filedata *fdata = arg;
  873. struct tid_rb_node *tnode =
  874. container_of(node, struct tid_rb_node, mmu);
  875. cacheless_tid_rb_remove(fdata, tnode);
  876. }