user_exp_rcv.c 30 KB

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