ipath_user_pages.c 5.3 KB

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