ipath_user_pages.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. 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 <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include "ipath_kernel.h"
  38. static void __ipath_release_user_pages(struct page **p, size_t num_pages,
  39. int dirty)
  40. {
  41. size_t i;
  42. for (i = 0; i < num_pages; i++) {
  43. ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i,
  44. (unsigned long) num_pages, p[i]);
  45. if (dirty)
  46. set_page_dirty_lock(p[i]);
  47. put_page(p[i]);
  48. }
  49. }
  50. /* call with current->mm->mmap_sem held */
  51. static int __ipath_get_user_pages(unsigned long start_page, size_t num_pages,
  52. struct page **p, struct vm_area_struct **vma)
  53. {
  54. unsigned long lock_limit;
  55. size_t got;
  56. int ret;
  57. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  58. if (num_pages > lock_limit) {
  59. ret = -ENOMEM;
  60. goto bail;
  61. }
  62. ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n",
  63. (unsigned long) num_pages, start_page);
  64. for (got = 0; got < num_pages; got += ret) {
  65. ret = get_user_pages(current, current->mm,
  66. start_page + got * PAGE_SIZE,
  67. num_pages - got, 1, 1,
  68. p + got, vma);
  69. if (ret < 0)
  70. goto bail_release;
  71. }
  72. current->mm->pinned_vm += num_pages;
  73. ret = 0;
  74. goto bail;
  75. bail_release:
  76. __ipath_release_user_pages(p, got, 0);
  77. bail:
  78. return ret;
  79. }
  80. /**
  81. * ipath_map_page - a safety wrapper around pci_map_page()
  82. *
  83. * A dma_addr of all 0's is interpreted by the chip as "disabled".
  84. * Unfortunately, it can also be a valid dma_addr returned on some
  85. * architectures.
  86. *
  87. * The powerpc iommu assigns dma_addrs in ascending order, so we don't
  88. * have to bother with retries or mapping a dummy page to insure we
  89. * don't just get the same mapping again.
  90. *
  91. * I'm sure we won't be so lucky with other iommu's, so FIXME.
  92. */
  93. dma_addr_t ipath_map_page(struct pci_dev *hwdev, struct page *page,
  94. unsigned long offset, size_t size, int direction)
  95. {
  96. dma_addr_t phys;
  97. phys = pci_map_page(hwdev, page, offset, size, direction);
  98. if (phys == 0) {
  99. pci_unmap_page(hwdev, phys, size, direction);
  100. phys = pci_map_page(hwdev, page, offset, size, direction);
  101. /*
  102. * FIXME: If we get 0 again, we should keep this page,
  103. * map another, then free the 0 page.
  104. */
  105. }
  106. return phys;
  107. }
  108. /**
  109. * ipath_map_single - a safety wrapper around pci_map_single()
  110. *
  111. * Same idea as ipath_map_page().
  112. */
  113. dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
  114. int direction)
  115. {
  116. dma_addr_t phys;
  117. phys = pci_map_single(hwdev, ptr, size, direction);
  118. if (phys == 0) {
  119. pci_unmap_single(hwdev, phys, size, direction);
  120. phys = pci_map_single(hwdev, ptr, size, direction);
  121. /*
  122. * FIXME: If we get 0 again, we should keep this page,
  123. * map another, then free the 0 page.
  124. */
  125. }
  126. return phys;
  127. }
  128. /**
  129. * ipath_get_user_pages - lock user pages into memory
  130. * @start_page: the start page
  131. * @num_pages: the number of pages
  132. * @p: the output page structures
  133. *
  134. * This function takes a given start page (page aligned user virtual
  135. * address) and pins it and the following specified number of pages. For
  136. * now, num_pages is always 1, but that will probably change at some point
  137. * (because caller is doing expected sends on a single virtually contiguous
  138. * buffer, so we can do all pages at once).
  139. */
  140. int ipath_get_user_pages(unsigned long start_page, size_t num_pages,
  141. struct page **p)
  142. {
  143. int ret;
  144. down_write(&current->mm->mmap_sem);
  145. ret = __ipath_get_user_pages(start_page, num_pages, p, NULL);
  146. up_write(&current->mm->mmap_sem);
  147. return ret;
  148. }
  149. void ipath_release_user_pages(struct page **p, size_t num_pages)
  150. {
  151. down_write(&current->mm->mmap_sem);
  152. __ipath_release_user_pages(p, num_pages, 1);
  153. current->mm->pinned_vm -= num_pages;
  154. up_write(&current->mm->mmap_sem);
  155. }
  156. struct ipath_user_pages_work {
  157. struct work_struct work;
  158. struct mm_struct *mm;
  159. unsigned long num_pages;
  160. };
  161. static void user_pages_account(struct work_struct *_work)
  162. {
  163. struct ipath_user_pages_work *work =
  164. container_of(_work, struct ipath_user_pages_work, work);
  165. down_write(&work->mm->mmap_sem);
  166. work->mm->pinned_vm -= work->num_pages;
  167. up_write(&work->mm->mmap_sem);
  168. mmput(work->mm);
  169. kfree(work);
  170. }
  171. void ipath_release_user_pages_on_close(struct page **p, size_t num_pages)
  172. {
  173. struct ipath_user_pages_work *work;
  174. struct mm_struct *mm;
  175. __ipath_release_user_pages(p, num_pages, 1);
  176. mm = get_task_mm(current);
  177. if (!mm)
  178. return;
  179. work = kmalloc(sizeof(*work), GFP_KERNEL);
  180. if (!work)
  181. goto bail_mm;
  182. INIT_WORK(&work->work, user_pages_account);
  183. work->mm = mm;
  184. work->num_pages = num_pages;
  185. queue_work(ib_wq, &work->work);
  186. return;
  187. bail_mm:
  188. mmput(mm);
  189. return;
  190. }