umem.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * Copyright (c) 2020 Intel Corporation. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/mm.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/sched/signal.h>
  38. #include <linux/sched/mm.h>
  39. #include <linux/export.h>
  40. #include <linux/slab.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/count_zeros.h>
  43. #include <rdma/ib_umem_odp.h>
  44. #include "uverbs.h"
  45. #define RESCHED_LOOP_CNT_THRESHOLD 0x1000
  46. static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
  47. {
  48. bool make_dirty = umem->writable && dirty;
  49. struct scatterlist *sg;
  50. unsigned int i;
  51. if (dirty)
  52. ib_dma_unmap_sgtable_attrs(dev, &umem->sgt_append.sgt,
  53. DMA_BIDIRECTIONAL,
  54. DMA_ATTR_REQUIRE_COHERENT);
  55. for_each_sgtable_sg(&umem->sgt_append.sgt, sg, i) {
  56. unpin_user_page_range_dirty_lock(sg_page(sg),
  57. DIV_ROUND_UP(sg->length, PAGE_SIZE), make_dirty);
  58. if (i && !(i % RESCHED_LOOP_CNT_THRESHOLD))
  59. cond_resched();
  60. }
  61. sg_free_append_table(&umem->sgt_append);
  62. }
  63. /**
  64. * ib_umem_find_best_pgsz - Find best HW page size to use for this MR
  65. *
  66. * @umem: umem struct
  67. * @pgsz_bitmap: bitmap of HW supported page sizes
  68. * @virt: IOVA
  69. *
  70. * This helper is intended for HW that support multiple page
  71. * sizes but can do only a single page size in an MR.
  72. *
  73. * Returns 0 if the umem requires page sizes not supported by
  74. * the driver to be mapped. Drivers always supporting PAGE_SIZE
  75. * or smaller will never see a 0 result.
  76. */
  77. unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
  78. unsigned long pgsz_bitmap,
  79. unsigned long virt)
  80. {
  81. unsigned long curr_len = 0;
  82. dma_addr_t curr_base = ~0;
  83. unsigned long va, pgoff;
  84. struct scatterlist *sg;
  85. dma_addr_t mask;
  86. dma_addr_t end;
  87. int i;
  88. umem->iova = va = virt;
  89. if (umem->is_odp) {
  90. unsigned int page_size = BIT(to_ib_umem_odp(umem)->page_shift);
  91. /* ODP must always be self consistent. */
  92. if (!(pgsz_bitmap & page_size))
  93. return 0;
  94. return page_size;
  95. }
  96. /* The best result is the smallest page size that results in the minimum
  97. * number of required pages. Compute the largest page size that could
  98. * work based on VA address bits that don't change.
  99. */
  100. mask = pgsz_bitmap &
  101. GENMASK(BITS_PER_LONG - 1,
  102. bits_per((umem->length - 1 + virt) ^ virt));
  103. /* offset into first SGL */
  104. pgoff = umem->address & ~PAGE_MASK;
  105. for_each_sgtable_dma_sg(&umem->sgt_append.sgt, sg, i) {
  106. /* If the current entry is physically contiguous with the previous
  107. * one, no need to take its start addresses into consideration.
  108. */
  109. if (check_add_overflow(curr_base, curr_len, &end) ||
  110. end != sg_dma_address(sg)) {
  111. curr_base = sg_dma_address(sg);
  112. curr_len = 0;
  113. /* Reduce max page size if VA/PA bits differ */
  114. mask |= (curr_base + pgoff) ^ va;
  115. /* The alignment of any VA matching a discontinuity point
  116. * in the physical memory sets the maximum possible page
  117. * size as this must be a starting point of a new page that
  118. * needs to be aligned.
  119. */
  120. if (i != 0)
  121. mask |= va;
  122. }
  123. curr_len += sg_dma_len(sg);
  124. va += sg_dma_len(sg) - pgoff;
  125. pgoff = 0;
  126. }
  127. /* The mask accumulates 1's in each position where the VA and physical
  128. * address differ, thus the length of trailing 0 is the largest page
  129. * size that can pass the VA through to the physical.
  130. */
  131. if (mask)
  132. pgsz_bitmap &= GENMASK(count_trailing_zeros(mask), 0);
  133. return pgsz_bitmap ? rounddown_pow_of_two(pgsz_bitmap) : 0;
  134. }
  135. EXPORT_SYMBOL(ib_umem_find_best_pgsz);
  136. /**
  137. * ib_umem_get - Pin and DMA map userspace memory.
  138. *
  139. * @device: IB device to connect UMEM
  140. * @addr: userspace virtual address to start at
  141. * @size: length of region to pin
  142. * @access: IB_ACCESS_xxx flags for memory being pinned
  143. */
  144. struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr,
  145. size_t size, int access)
  146. {
  147. struct ib_umem *umem;
  148. struct page **page_list;
  149. unsigned long lock_limit;
  150. unsigned long new_pinned;
  151. unsigned long cur_base;
  152. unsigned long dma_attr = DMA_ATTR_REQUIRE_COHERENT;
  153. struct mm_struct *mm;
  154. unsigned long npages;
  155. int pinned, ret;
  156. unsigned int gup_flags = FOLL_LONGTERM;
  157. /*
  158. * If the combination of the addr and size requested for this memory
  159. * region causes an integer overflow, return error.
  160. */
  161. if (((addr + size) < addr) ||
  162. PAGE_ALIGN(addr + size) < (addr + size))
  163. return ERR_PTR(-EINVAL);
  164. if (!can_do_mlock())
  165. return ERR_PTR(-EPERM);
  166. if (access & IB_ACCESS_ON_DEMAND)
  167. return ERR_PTR(-EOPNOTSUPP);
  168. umem = kzalloc_obj(*umem);
  169. if (!umem)
  170. return ERR_PTR(-ENOMEM);
  171. umem->ibdev = device;
  172. umem->length = size;
  173. umem->address = addr;
  174. /*
  175. * Drivers should call ib_umem_find_best_pgsz() to set the iova
  176. * correctly.
  177. */
  178. umem->iova = addr;
  179. umem->writable = ib_access_writable(access);
  180. umem->owning_mm = mm = current->mm;
  181. mmgrab(mm);
  182. page_list = (struct page **) __get_free_page(GFP_KERNEL);
  183. if (!page_list) {
  184. ret = -ENOMEM;
  185. goto umem_kfree;
  186. }
  187. npages = ib_umem_num_pages(umem);
  188. if (npages == 0 || npages > UINT_MAX) {
  189. ret = -EINVAL;
  190. goto out;
  191. }
  192. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  193. new_pinned = atomic64_add_return(npages, &mm->pinned_vm);
  194. if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
  195. atomic64_sub(npages, &mm->pinned_vm);
  196. ret = -ENOMEM;
  197. goto out;
  198. }
  199. cur_base = addr & PAGE_MASK;
  200. if (umem->writable)
  201. gup_flags |= FOLL_WRITE;
  202. while (npages) {
  203. cond_resched();
  204. pinned = pin_user_pages_fast(cur_base,
  205. min_t(unsigned long, npages,
  206. PAGE_SIZE /
  207. sizeof(struct page *)),
  208. gup_flags, page_list);
  209. if (pinned < 0) {
  210. ret = pinned;
  211. goto umem_release;
  212. }
  213. cur_base += pinned * PAGE_SIZE;
  214. npages -= pinned;
  215. ret = sg_alloc_append_table_from_pages(
  216. &umem->sgt_append, page_list, pinned, 0,
  217. pinned << PAGE_SHIFT, ib_dma_max_seg_size(device),
  218. npages, GFP_KERNEL);
  219. if (ret) {
  220. unpin_user_pages_dirty_lock(page_list, pinned, 0);
  221. goto umem_release;
  222. }
  223. }
  224. if (access & IB_ACCESS_RELAXED_ORDERING)
  225. dma_attr |= DMA_ATTR_WEAK_ORDERING;
  226. ret = ib_dma_map_sgtable_attrs(device, &umem->sgt_append.sgt,
  227. DMA_BIDIRECTIONAL, dma_attr);
  228. if (ret)
  229. goto umem_release;
  230. goto out;
  231. umem_release:
  232. __ib_umem_release(device, umem, 0);
  233. atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm);
  234. out:
  235. free_page((unsigned long) page_list);
  236. umem_kfree:
  237. if (ret) {
  238. mmdrop(umem->owning_mm);
  239. kfree(umem);
  240. }
  241. return ret ? ERR_PTR(ret) : umem;
  242. }
  243. EXPORT_SYMBOL(ib_umem_get);
  244. /**
  245. * ib_umem_release - release memory pinned with ib_umem_get
  246. * @umem: umem struct to release
  247. */
  248. void ib_umem_release(struct ib_umem *umem)
  249. {
  250. if (!umem)
  251. return;
  252. if (umem->is_dmabuf)
  253. return ib_umem_dmabuf_release(to_ib_umem_dmabuf(umem));
  254. if (umem->is_odp)
  255. return ib_umem_odp_release(to_ib_umem_odp(umem));
  256. __ib_umem_release(umem->ibdev, umem, 1);
  257. atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm);
  258. mmdrop(umem->owning_mm);
  259. kfree(umem);
  260. }
  261. EXPORT_SYMBOL(ib_umem_release);
  262. /*
  263. * Copy from the given ib_umem's pages to the given buffer.
  264. *
  265. * umem - the umem to copy from
  266. * offset - offset to start copying from
  267. * dst - destination buffer
  268. * length - buffer length
  269. *
  270. * Returns 0 on success, or an error code.
  271. */
  272. int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
  273. size_t length)
  274. {
  275. size_t end = offset + length;
  276. int ret;
  277. if (offset > umem->length || length > umem->length - offset) {
  278. pr_err("%s not in range. offset: %zd umem length: %zd end: %zd\n",
  279. __func__, offset, umem->length, end);
  280. return -EINVAL;
  281. }
  282. ret = sg_pcopy_to_buffer(umem->sgt_append.sgt.sgl,
  283. umem->sgt_append.sgt.orig_nents, dst, length,
  284. offset + ib_umem_offset(umem));
  285. if (ret < 0)
  286. return ret;
  287. else if (ret != length)
  288. return -EINVAL;
  289. else
  290. return 0;
  291. }
  292. EXPORT_SYMBOL(ib_umem_copy_from);