ipath_user_pages.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/mm.h>
  34. #include <linux/device.h>
  35. #include "ipath_kernel.h"
  36. static void __ipath_release_user_pages(struct page **p, size_t num_pages,
  37. int dirty)
  38. {
  39. size_t i;
  40. for (i = 0; i < num_pages; i++) {
  41. ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i,
  42. (unsigned long) num_pages, p[i]);
  43. if (dirty)
  44. set_page_dirty_lock(p[i]);
  45. put_page(p[i]);
  46. }
  47. }
  48. /* call with current->mm->mmap_sem held */
  49. static int __get_user_pages(unsigned long start_page, size_t num_pages,
  50. struct page **p, struct vm_area_struct **vma)
  51. {
  52. unsigned long lock_limit;
  53. size_t got;
  54. int ret;
  55. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >>
  56. PAGE_SHIFT;
  57. if (num_pages > lock_limit) {
  58. ret = -ENOMEM;
  59. goto bail;
  60. }
  61. ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n",
  62. (unsigned long) num_pages, start_page);
  63. for (got = 0; got < num_pages; got += ret) {
  64. ret = get_user_pages(current, current->mm,
  65. start_page + got * PAGE_SIZE,
  66. num_pages - got, 1, 1,
  67. p + got, vma);
  68. if (ret < 0)
  69. goto bail_release;
  70. }
  71. current->mm->locked_vm += num_pages;
  72. ret = 0;
  73. goto bail;
  74. bail_release:
  75. __ipath_release_user_pages(p, got, 0);
  76. bail:
  77. return ret;
  78. }
  79. /**
  80. * ipath_map_page - a safety wrapper around pci_map_page()
  81. *
  82. * A dma_addr of all 0's is interpreted by the chip as "disabled".
  83. * Unfortunately, it can also be a valid dma_addr returned on some
  84. * architectures.
  85. *
  86. * The powerpc iommu assigns dma_addrs in ascending order, so we don't
  87. * have to bother with retries or mapping a dummy page to insure we
  88. * don't just get the same mapping again.
  89. *
  90. * I'm sure we won't be so lucky with other iommu's, so FIXME.
  91. */
  92. dma_addr_t ipath_map_page(struct pci_dev *hwdev, struct page *page,
  93. unsigned long offset, size_t size, int direction)
  94. {
  95. dma_addr_t phys;
  96. phys = pci_map_page(hwdev, page, offset, size, direction);
  97. if (phys == 0) {
  98. pci_unmap_page(hwdev, phys, size, direction);
  99. phys = pci_map_page(hwdev, page, offset, size, direction);
  100. /*
  101. * FIXME: If we get 0 again, we should keep this page,
  102. * map another, then free the 0 page.
  103. */
  104. }
  105. return phys;
  106. }
  107. /**
  108. * ipath_map_single - a safety wrapper around pci_map_single()
  109. *
  110. * Same idea as ipath_map_page().
  111. */
  112. dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
  113. int direction)
  114. {
  115. dma_addr_t phys;
  116. phys = pci_map_single(hwdev, ptr, size, direction);
  117. if (phys == 0) {
  118. pci_unmap_single(hwdev, phys, size, direction);
  119. phys = pci_map_single(hwdev, ptr, size, direction);
  120. /*
  121. * FIXME: If we get 0 again, we should keep this page,
  122. * map another, then free the 0 page.
  123. */
  124. }
  125. return phys;
  126. }
  127. /**
  128. * ipath_get_user_pages - lock user pages into memory
  129. * @start_page: the start page
  130. * @num_pages: the number of pages
  131. * @p: the output page structures
  132. *
  133. * This function takes a given start page (page aligned user virtual
  134. * address) and pins it and the following specified number of pages. For
  135. * now, num_pages is always 1, but that will probably change at some point
  136. * (because caller is doing expected sends on a single virtually contiguous
  137. * buffer, so we can do all pages at once).
  138. */
  139. int ipath_get_user_pages(unsigned long start_page, size_t num_pages,
  140. struct page **p)
  141. {
  142. int ret;
  143. down_write(&current->mm->mmap_sem);
  144. ret = __get_user_pages(start_page, num_pages, p, NULL);
  145. up_write(&current->mm->mmap_sem);
  146. return ret;
  147. }
  148. /**
  149. * ipath_get_user_pages_nocopy - lock a single page for I/O and mark shared
  150. * @start_page: the page to lock
  151. * @p: the output page structure
  152. *
  153. * This is similar to ipath_get_user_pages, but it's always one page, and we
  154. * mark the page as locked for I/O, and shared. This is used for the user
  155. * process page that contains the destination address for the rcvhdrq tail
  156. * update, so we need to have the vma. If we don't do this, the page can be
  157. * taken away from us on fork, even if the child never touches it, and then
  158. * the user process never sees the tail register updates.
  159. */
  160. int ipath_get_user_pages_nocopy(unsigned long page, struct page **p)
  161. {
  162. struct vm_area_struct *vma;
  163. int ret;
  164. down_write(&current->mm->mmap_sem);
  165. ret = __get_user_pages(page, 1, p, &vma);
  166. up_write(&current->mm->mmap_sem);
  167. return ret;
  168. }
  169. void ipath_release_user_pages(struct page **p, size_t num_pages)
  170. {
  171. down_write(&current->mm->mmap_sem);
  172. __ipath_release_user_pages(p, num_pages, 1);
  173. current->mm->locked_vm -= num_pages;
  174. up_write(&current->mm->mmap_sem);
  175. }
  176. struct ipath_user_pages_work {
  177. struct work_struct work;
  178. struct mm_struct *mm;
  179. unsigned long num_pages;
  180. };
  181. static void user_pages_account(void *ptr)
  182. {
  183. struct ipath_user_pages_work *work = ptr;
  184. down_write(&work->mm->mmap_sem);
  185. work->mm->locked_vm -= work->num_pages;
  186. up_write(&work->mm->mmap_sem);
  187. mmput(work->mm);
  188. kfree(work);
  189. }
  190. void ipath_release_user_pages_on_close(struct page **p, size_t num_pages)
  191. {
  192. struct ipath_user_pages_work *work;
  193. struct mm_struct *mm;
  194. __ipath_release_user_pages(p, num_pages, 1);
  195. mm = get_task_mm(current);
  196. if (!mm)
  197. goto bail;
  198. work = kmalloc(sizeof(*work), GFP_KERNEL);
  199. if (!work)
  200. goto bail_mm;
  201. goto bail;
  202. INIT_WORK(&work->work, user_pages_account, work);
  203. work->mm = mm;
  204. work->num_pages = num_pages;
  205. bail_mm:
  206. mmput(mm);
  207. bail:
  208. return;
  209. }