user_exp_rcv.c 30 KB

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