uverbs_mem.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * $Id: uverbs_mem.c 2743 2005-06-28 22:27:59Z roland $
  35. */
  36. #include <linux/mm.h>
  37. #include <linux/dma-mapping.h>
  38. #include "uverbs.h"
  39. struct ib_umem_account_work {
  40. struct work_struct work;
  41. struct mm_struct *mm;
  42. unsigned long diff;
  43. };
  44. static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
  45. {
  46. struct ib_umem_chunk *chunk, *tmp;
  47. int i;
  48. list_for_each_entry_safe(chunk, tmp, &umem->chunk_list, list) {
  49. dma_unmap_sg(dev->dma_device, chunk->page_list,
  50. chunk->nents, DMA_BIDIRECTIONAL);
  51. for (i = 0; i < chunk->nents; ++i) {
  52. if (umem->writable && dirty)
  53. set_page_dirty_lock(chunk->page_list[i].page);
  54. put_page(chunk->page_list[i].page);
  55. }
  56. kfree(chunk);
  57. }
  58. }
  59. int ib_umem_get(struct ib_device *dev, struct ib_umem *mem,
  60. void *addr, size_t size, int write)
  61. {
  62. struct page **page_list;
  63. struct ib_umem_chunk *chunk;
  64. unsigned long locked;
  65. unsigned long lock_limit;
  66. unsigned long cur_base;
  67. unsigned long npages;
  68. int ret = 0;
  69. int off;
  70. int i;
  71. if (!can_do_mlock())
  72. return -EPERM;
  73. page_list = (struct page **) __get_free_page(GFP_KERNEL);
  74. if (!page_list)
  75. return -ENOMEM;
  76. mem->user_base = (unsigned long) addr;
  77. mem->length = size;
  78. mem->offset = (unsigned long) addr & ~PAGE_MASK;
  79. mem->page_size = PAGE_SIZE;
  80. mem->writable = write;
  81. INIT_LIST_HEAD(&mem->chunk_list);
  82. npages = PAGE_ALIGN(size + mem->offset) >> PAGE_SHIFT;
  83. down_write(&current->mm->mmap_sem);
  84. locked = npages + current->mm->locked_vm;
  85. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  86. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  87. ret = -ENOMEM;
  88. goto out;
  89. }
  90. cur_base = (unsigned long) addr & PAGE_MASK;
  91. while (npages) {
  92. ret = get_user_pages(current, current->mm, cur_base,
  93. min_t(int, npages,
  94. PAGE_SIZE / sizeof (struct page *)),
  95. 1, !write, page_list, NULL);
  96. if (ret < 0)
  97. goto out;
  98. cur_base += ret * PAGE_SIZE;
  99. npages -= ret;
  100. off = 0;
  101. while (ret) {
  102. chunk = kmalloc(sizeof *chunk + sizeof (struct scatterlist) *
  103. min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK),
  104. GFP_KERNEL);
  105. if (!chunk) {
  106. ret = -ENOMEM;
  107. goto out;
  108. }
  109. chunk->nents = min_t(int, ret, IB_UMEM_MAX_PAGE_CHUNK);
  110. for (i = 0; i < chunk->nents; ++i) {
  111. chunk->page_list[i].page = page_list[i + off];
  112. chunk->page_list[i].offset = 0;
  113. chunk->page_list[i].length = PAGE_SIZE;
  114. }
  115. chunk->nmap = dma_map_sg(dev->dma_device,
  116. &chunk->page_list[0],
  117. chunk->nents,
  118. DMA_BIDIRECTIONAL);
  119. if (chunk->nmap <= 0) {
  120. for (i = 0; i < chunk->nents; ++i)
  121. put_page(chunk->page_list[i].page);
  122. kfree(chunk);
  123. ret = -ENOMEM;
  124. goto out;
  125. }
  126. ret -= chunk->nents;
  127. off += chunk->nents;
  128. list_add_tail(&chunk->list, &mem->chunk_list);
  129. }
  130. ret = 0;
  131. }
  132. out:
  133. if (ret < 0)
  134. __ib_umem_release(dev, mem, 0);
  135. else
  136. current->mm->locked_vm = locked;
  137. up_write(&current->mm->mmap_sem);
  138. free_page((unsigned long) page_list);
  139. return ret;
  140. }
  141. void ib_umem_release(struct ib_device *dev, struct ib_umem *umem)
  142. {
  143. __ib_umem_release(dev, umem, 1);
  144. down_write(&current->mm->mmap_sem);
  145. current->mm->locked_vm -=
  146. PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
  147. up_write(&current->mm->mmap_sem);
  148. }
  149. static void ib_umem_account(void *work_ptr)
  150. {
  151. struct ib_umem_account_work *work = work_ptr;
  152. down_write(&work->mm->mmap_sem);
  153. work->mm->locked_vm -= work->diff;
  154. up_write(&work->mm->mmap_sem);
  155. mmput(work->mm);
  156. kfree(work);
  157. }
  158. void ib_umem_release_on_close(struct ib_device *dev, struct ib_umem *umem)
  159. {
  160. struct ib_umem_account_work *work;
  161. struct mm_struct *mm;
  162. __ib_umem_release(dev, umem, 1);
  163. mm = get_task_mm(current);
  164. if (!mm)
  165. return;
  166. /*
  167. * We may be called with the mm's mmap_sem already held. This
  168. * can happen when a userspace munmap() is the call that drops
  169. * the last reference to our file and calls our release
  170. * method. If there are memory regions to destroy, we'll end
  171. * up here and not be able to take the mmap_sem. Therefore we
  172. * defer the vm_locked accounting to the system workqueue.
  173. */
  174. work = kmalloc(sizeof *work, GFP_KERNEL);
  175. if (!work)
  176. return;
  177. INIT_WORK(&work->work, ib_umem_account, work);
  178. work->mm = mm;
  179. work->diff = PAGE_ALIGN(umem->length + umem->offset) >> PAGE_SHIFT;
  180. schedule_work(&work->work);
  181. }