umem.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #include <linux/mm.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/sched/signal.h>
  37. #include <linux/sched/mm.h>
  38. #include <linux/export.h>
  39. #include <linux/slab.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/count_zeros.h>
  42. #include <rdma/ib_umem_odp.h>
  43. #include "uverbs.h"
  44. static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
  45. {
  46. struct sg_page_iter sg_iter;
  47. struct page *page;
  48. if (umem->nmap > 0)
  49. ib_dma_unmap_sg(dev, umem->sg_head.sgl, umem->sg_nents,
  50. DMA_BIDIRECTIONAL);
  51. for_each_sg_page(umem->sg_head.sgl, &sg_iter, umem->sg_nents, 0) {
  52. page = sg_page_iter_page(&sg_iter);
  53. unpin_user_pages_dirty_lock(&page, 1, umem->writable && dirty);
  54. }
  55. sg_free_table(&umem->sg_head);
  56. }
  57. /**
  58. * ib_umem_find_best_pgsz - Find best HW page size to use for this MR
  59. *
  60. * @umem: umem struct
  61. * @pgsz_bitmap: bitmap of HW supported page sizes
  62. * @virt: IOVA
  63. *
  64. * This helper is intended for HW that support multiple page
  65. * sizes but can do only a single page size in an MR.
  66. *
  67. * Returns 0 if the umem requires page sizes not supported by
  68. * the driver to be mapped. Drivers always supporting PAGE_SIZE
  69. * or smaller will never see a 0 result.
  70. */
  71. unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
  72. unsigned long pgsz_bitmap,
  73. unsigned long virt)
  74. {
  75. struct scatterlist *sg;
  76. unsigned long va, pgoff;
  77. dma_addr_t mask;
  78. int i;
  79. /* rdma_for_each_block() has a bug if the page size is smaller than the
  80. * page size used to build the umem. For now prevent smaller page sizes
  81. * from being returned.
  82. */
  83. pgsz_bitmap &= GENMASK(BITS_PER_LONG - 1, PAGE_SHIFT);
  84. /* At minimum, drivers must support PAGE_SIZE or smaller */
  85. if (WARN_ON(!(pgsz_bitmap & GENMASK(PAGE_SHIFT, 0))))
  86. return 0;
  87. umem->iova = va = virt;
  88. /* The best result is the smallest page size that results in the minimum
  89. * number of required pages. Compute the largest page size that could
  90. * work based on VA address bits that don't change.
  91. */
  92. mask = pgsz_bitmap &
  93. GENMASK(BITS_PER_LONG - 1,
  94. bits_per((umem->length - 1 + virt) ^ virt));
  95. /* offset into first SGL */
  96. pgoff = umem->address & ~PAGE_MASK;
  97. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, i) {
  98. /* Walk SGL and reduce max page size if VA/PA bits differ
  99. * for any address.
  100. */
  101. mask |= (sg_dma_address(sg) + pgoff) ^ va;
  102. va += sg_dma_len(sg) - pgoff;
  103. /* Except for the last entry, the ending iova alignment sets
  104. * the maximum possible page size as the low bits of the iova
  105. * must be zero when starting the next chunk.
  106. */
  107. if (i != (umem->nmap - 1))
  108. mask |= va;
  109. pgoff = 0;
  110. }
  111. /* The mask accumulates 1's in each position where the VA and physical
  112. * address differ, thus the length of trailing 0 is the largest page
  113. * size that can pass the VA through to the physical.
  114. */
  115. if (mask)
  116. pgsz_bitmap &= GENMASK(count_trailing_zeros(mask), 0);
  117. return pgsz_bitmap ? rounddown_pow_of_two(pgsz_bitmap) : 0;
  118. }
  119. EXPORT_SYMBOL(ib_umem_find_best_pgsz);
  120. /**
  121. * ib_umem_get - Pin and DMA map userspace memory.
  122. *
  123. * @device: IB device to connect UMEM
  124. * @addr: userspace virtual address to start at
  125. * @size: length of region to pin
  126. * @access: IB_ACCESS_xxx flags for memory being pinned
  127. */
  128. struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr,
  129. size_t size, int access)
  130. {
  131. struct ib_umem *umem;
  132. struct page **page_list;
  133. unsigned long lock_limit;
  134. unsigned long new_pinned;
  135. unsigned long cur_base;
  136. unsigned long dma_attr = 0;
  137. struct mm_struct *mm;
  138. unsigned long npages;
  139. int ret;
  140. struct scatterlist *sg = NULL;
  141. unsigned int gup_flags = FOLL_WRITE;
  142. /*
  143. * If the combination of the addr and size requested for this memory
  144. * region causes an integer overflow, return error.
  145. */
  146. if (((addr + size) < addr) ||
  147. PAGE_ALIGN(addr + size) < (addr + size))
  148. return ERR_PTR(-EINVAL);
  149. if (!can_do_mlock())
  150. return ERR_PTR(-EPERM);
  151. if (access & IB_ACCESS_ON_DEMAND)
  152. return ERR_PTR(-EOPNOTSUPP);
  153. umem = kzalloc(sizeof(*umem), GFP_KERNEL);
  154. if (!umem)
  155. return ERR_PTR(-ENOMEM);
  156. umem->ibdev = device;
  157. umem->length = size;
  158. umem->address = addr;
  159. /*
  160. * Drivers should call ib_umem_find_best_pgsz() to set the iova
  161. * correctly.
  162. */
  163. umem->iova = addr;
  164. umem->writable = ib_access_writable(access);
  165. umem->owning_mm = mm = current->mm;
  166. mmgrab(mm);
  167. page_list = (struct page **) __get_free_page(GFP_KERNEL);
  168. if (!page_list) {
  169. ret = -ENOMEM;
  170. goto umem_kfree;
  171. }
  172. npages = ib_umem_num_pages(umem);
  173. if (npages == 0 || npages > UINT_MAX) {
  174. ret = -EINVAL;
  175. goto out;
  176. }
  177. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  178. new_pinned = atomic64_add_return(npages, &mm->pinned_vm);
  179. if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
  180. atomic64_sub(npages, &mm->pinned_vm);
  181. ret = -ENOMEM;
  182. goto out;
  183. }
  184. cur_base = addr & PAGE_MASK;
  185. if (!umem->writable)
  186. gup_flags |= FOLL_FORCE;
  187. while (npages) {
  188. cond_resched();
  189. ret = pin_user_pages_fast(cur_base,
  190. min_t(unsigned long, npages,
  191. PAGE_SIZE /
  192. sizeof(struct page *)),
  193. gup_flags | FOLL_LONGTERM, page_list);
  194. if (ret < 0)
  195. goto umem_release;
  196. cur_base += ret * PAGE_SIZE;
  197. npages -= ret;
  198. sg = __sg_alloc_table_from_pages(&umem->sg_head, page_list, ret,
  199. 0, ret << PAGE_SHIFT,
  200. ib_dma_max_seg_size(device), sg, npages,
  201. GFP_KERNEL);
  202. umem->sg_nents = umem->sg_head.nents;
  203. if (IS_ERR(sg)) {
  204. unpin_user_pages_dirty_lock(page_list, ret, 0);
  205. ret = PTR_ERR(sg);
  206. goto umem_release;
  207. }
  208. }
  209. if (access & IB_ACCESS_RELAXED_ORDERING)
  210. dma_attr |= DMA_ATTR_WEAK_ORDERING;
  211. umem->nmap =
  212. ib_dma_map_sg_attrs(device, umem->sg_head.sgl, umem->sg_nents,
  213. DMA_BIDIRECTIONAL, dma_attr);
  214. if (!umem->nmap) {
  215. ret = -ENOMEM;
  216. goto umem_release;
  217. }
  218. ret = 0;
  219. goto out;
  220. umem_release:
  221. __ib_umem_release(device, umem, 0);
  222. atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm);
  223. out:
  224. free_page((unsigned long) page_list);
  225. umem_kfree:
  226. if (ret) {
  227. mmdrop(umem->owning_mm);
  228. kfree(umem);
  229. }
  230. return ret ? ERR_PTR(ret) : umem;
  231. }
  232. EXPORT_SYMBOL(ib_umem_get);
  233. /**
  234. * ib_umem_release - release memory pinned with ib_umem_get
  235. * @umem: umem struct to release
  236. */
  237. void ib_umem_release(struct ib_umem *umem)
  238. {
  239. if (!umem)
  240. return;
  241. if (umem->is_odp)
  242. return ib_umem_odp_release(to_ib_umem_odp(umem));
  243. __ib_umem_release(umem->ibdev, umem, 1);
  244. atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm);
  245. mmdrop(umem->owning_mm);
  246. kfree(umem);
  247. }
  248. EXPORT_SYMBOL(ib_umem_release);
  249. /*
  250. * Copy from the given ib_umem's pages to the given buffer.
  251. *
  252. * umem - the umem to copy from
  253. * offset - offset to start copying from
  254. * dst - destination buffer
  255. * length - buffer length
  256. *
  257. * Returns 0 on success, or an error code.
  258. */
  259. int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
  260. size_t length)
  261. {
  262. size_t end = offset + length;
  263. int ret;
  264. if (offset > umem->length || length > umem->length - offset) {
  265. pr_err("ib_umem_copy_from not in range. offset: %zd umem length: %zd end: %zd\n",
  266. offset, umem->length, end);
  267. return -EINVAL;
  268. }
  269. ret = sg_pcopy_to_buffer(umem->sg_head.sgl, umem->sg_nents, dst, length,
  270. offset + ib_umem_offset(umem));
  271. if (ret < 0)
  272. return ret;
  273. else if (ret != length)
  274. return -EINVAL;
  275. else
  276. return 0;
  277. }
  278. EXPORT_SYMBOL(ib_umem_copy_from);