user_exp_rcv.c 30 KB

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