uverbs_mem.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. 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. * $Id: uverbs_mem.c 2743 2005-06-28 22:27:59Z roland $
  34. */
  35. #include <linux/mm.h>
  36. #include <linux/dma-mapping.h>
  37. #include "uverbs.h"
  38. struct ib_umem_account_work {
  39. struct work_struct work;
  40. struct mm_struct *mm;
  41. unsigned long diff;
  42. };
  43. static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
  44. {
  45. struct ib_umem_chunk *chunk, *tmp;
  46. int i;
  47. list_for_each_entry_safe(chunk, tmp, &umem->chunk_list, list) {
  48. dma_unmap_sg(dev->dma_device, chunk->page_list,
  49. chunk->nents, DMA_BIDIRECTIONAL);
  50. for (i = 0; i < chunk->nents; ++i) {
  51. if (umem->writable && dirty)
  52. set_page_dirty_lock(chunk->page_list[i].page);
  53. put_page(chunk->page_list[i].page);
  54. }
  55. kfree(chunk);
  56. }
  57. }
  58. int ib_umem_get(struct ib_device *dev, struct ib_umem *mem,
  59. void *addr, size_t size, int write)
  60. {
  61. struct page **page_list;
  62. struct ib_umem_chunk *chunk;
  63. unsigned long locked;
  64. unsigned long lock_limit;
  65. unsigned long cur_base;
  66. unsigned long npages;
  67. int ret = 0;
  68. int off;
  69. int i;
  70. if (!can_do_mlock())
  71. return -EPERM;
  72. page_list = (struct page **) __get_free_page(GFP_KERNEL);
  73. if (!page_list)
  74. return -ENOMEM;
  75. mem->user_base = (unsigned long) addr;
  76. mem->length = size;
  77. mem->offset = (unsigned long) addr & ~PAGE_MASK;
  78. mem->page_size = PAGE_SIZE;
  79. mem->writable = write;
  80. INIT_LIST_HEAD(&mem->chunk_list);
  81. npages = PAGE_ALIGN(size + mem->offset) >> PAGE_SHIFT;
  82. down_write(&current->mm->mmap_sem);
  83. locked = npages + current->mm->locked_vm;
  84. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  85. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  86. ret = -ENOMEM;
  87. goto out;
  88. }
  89. cur_base = (unsigned long) addr & PAGE_MASK;
  90. while (npages) {
  91. ret = get_user_pages(current, current->mm, cur_base,
  92. min_t(int, npages,
  93. PAGE_SIZE / sizeof (struct page *)),
  94. 1, !write, page_list, NULL);
  95. if (ret < 0)
  96. goto out;
  97. cur_base += ret * PAGE_SIZE;
  98. npages -= ret;
  99. off = 0;
  100. while (ret) {
  101. chunk = kmalloc(sizeof *chunk + sizeof (struct scatterlist) *
  102. min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK),
  103. GFP_KERNEL);
  104. if (!chunk) {
  105. ret = -ENOMEM;
  106. goto out;
  107. }
  108. chunk->nents = min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK);
  109. for (i = 0; i < chunk->nents; ++i) {
  110. chunk->page_list[i].page = page_list[i + off];
  111. chunk->page_list[i].offset = 0;
  112. chunk->page_list[i].length = PAGE_SIZE;
  113. }
  114. chunk->nmap = dma_map_sg(dev->dma_device,
  115. &chunk->page_list[0],
  116. chunk->nents,
  117. DMA_BIDIRECTIONAL);
  118. if (chunk->nmap <= 0) {
  119. for (i = 0; i < chunk->nents; ++i)
  120. put_page(chunk->page_list[i].page);
  121. kfree(chunk);
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. ret -= chunk->nents;
  126. off += chunk->nents;
  127. list_add_tail(&chunk->list, &mem->chunk_list);
  128. }
  129. ret = 0;
  130. }
  131. out:
  132. if (ret < 0)
  133. __ib_umem_release(dev, mem, 0);
  134. else
  135. current->mm->locked_vm = locked;
  136. up_write(&current->mm->mmap_sem);
  137. free_page((unsigned long) page_list);
  138. return ret;
  139. }
  140. void ib_umem_release(struct ib_device *dev, struct ib_umem *umem)
  141. {
  142. __ib_umem_release(dev, umem, 1);
  143. down_write(&current->mm->mmap_sem);
  144. current->mm->locked_vm -=
  145. PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
  146. up_write(&current->mm->mmap_sem);
  147. }
  148. static void ib_umem_account(void *work_ptr)
  149. {
  150. struct ib_umem_account_work *work = work_ptr;
  151. down_write(&work->mm->mmap_sem);
  152. work->mm->locked_vm -= work->diff;
  153. up_write(&work->mm->mmap_sem);
  154. mmput(work->mm);
  155. kfree(work);
  156. }
  157. void ib_umem_release_on_close(struct ib_device *dev, struct ib_umem *umem)
  158. {
  159. struct ib_umem_account_work *work;
  160. struct mm_struct *mm;
  161. __ib_umem_release(dev, umem, 1);
  162. mm = get_task_mm(current);
  163. if (!mm)
  164. return;
  165. /*
  166. * We may be called with the mm's mmap_sem already held. This
  167. * can happen when a userspace munmap() is the call that drops
  168. * the last reference to our file and calls our release
  169. * method. If there are memory regions to destroy, we'll end
  170. * up here and not be able to take the mmap_sem. Therefore we
  171. * defer the vm_locked accounting to the system workqueue.
  172. */
  173. work = kmalloc(sizeof *work, GFP_KERNEL);
  174. if (!work)
  175. return;
  176. INIT_WORK(&work->work, ib_umem_account, work);
  177. work->mm = mm;
  178. work->diff = PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
  179. schedule_work(&work->work);
  180. }