ib_fmr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2016 Oracle. 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 "ib_mr.h"
  33. struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev, int npages)
  34. {
  35. struct rds_ib_mr_pool *pool;
  36. struct rds_ib_mr *ibmr = NULL;
  37. struct rds_ib_fmr *fmr;
  38. int err = 0;
  39. if (npages <= RDS_MR_8K_MSG_SIZE)
  40. pool = rds_ibdev->mr_8k_pool;
  41. else
  42. pool = rds_ibdev->mr_1m_pool;
  43. if (atomic_read(&pool->dirty_count) >= pool->max_items / 10)
  44. queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
  45. /* Switch pools if one of the pool is reaching upper limit */
  46. if (atomic_read(&pool->dirty_count) >= pool->max_items * 9 / 10) {
  47. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  48. pool = rds_ibdev->mr_1m_pool;
  49. else
  50. pool = rds_ibdev->mr_8k_pool;
  51. }
  52. ibmr = rds_ib_try_reuse_ibmr(pool);
  53. if (ibmr)
  54. return ibmr;
  55. ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL,
  56. rdsibdev_to_node(rds_ibdev));
  57. if (!ibmr) {
  58. err = -ENOMEM;
  59. goto out_no_cigar;
  60. }
  61. fmr = &ibmr->u.fmr;
  62. fmr->fmr = ib_alloc_fmr(rds_ibdev->pd,
  63. (IB_ACCESS_LOCAL_WRITE |
  64. IB_ACCESS_REMOTE_READ |
  65. IB_ACCESS_REMOTE_WRITE |
  66. IB_ACCESS_REMOTE_ATOMIC),
  67. &pool->fmr_attr);
  68. if (IS_ERR(fmr->fmr)) {
  69. err = PTR_ERR(fmr->fmr);
  70. fmr->fmr = NULL;
  71. pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, err);
  72. goto out_no_cigar;
  73. }
  74. ibmr->pool = pool;
  75. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  76. rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
  77. else
  78. rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
  79. return ibmr;
  80. out_no_cigar:
  81. kfree(ibmr);
  82. atomic_dec(&pool->item_count);
  83. return ERR_PTR(err);
  84. }
  85. static int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev,
  86. struct rds_ib_mr *ibmr, struct scatterlist *sg,
  87. unsigned int nents)
  88. {
  89. struct ib_device *dev = rds_ibdev->dev;
  90. struct rds_ib_fmr *fmr = &ibmr->u.fmr;
  91. struct scatterlist *scat = sg;
  92. u64 io_addr = 0;
  93. u64 *dma_pages;
  94. u32 len;
  95. int page_cnt, sg_dma_len;
  96. int i, j;
  97. int ret;
  98. sg_dma_len = ib_dma_map_sg(dev, sg, nents, DMA_BIDIRECTIONAL);
  99. if (unlikely(!sg_dma_len)) {
  100. pr_warn("RDS/IB: %s failed!\n", __func__);
  101. return -EBUSY;
  102. }
  103. len = 0;
  104. page_cnt = 0;
  105. for (i = 0; i < sg_dma_len; ++i) {
  106. unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
  107. u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
  108. if (dma_addr & ~PAGE_MASK) {
  109. if (i > 0) {
  110. ib_dma_unmap_sg(dev, sg, nents,
  111. DMA_BIDIRECTIONAL);
  112. return -EINVAL;
  113. } else {
  114. ++page_cnt;
  115. }
  116. }
  117. if ((dma_addr + dma_len) & ~PAGE_MASK) {
  118. if (i < sg_dma_len - 1) {
  119. ib_dma_unmap_sg(dev, sg, nents,
  120. DMA_BIDIRECTIONAL);
  121. return -EINVAL;
  122. } else {
  123. ++page_cnt;
  124. }
  125. }
  126. len += dma_len;
  127. }
  128. page_cnt += len >> PAGE_SHIFT;
  129. if (page_cnt > ibmr->pool->fmr_attr.max_pages) {
  130. ib_dma_unmap_sg(dev, sg, nents, DMA_BIDIRECTIONAL);
  131. return -EINVAL;
  132. }
  133. dma_pages = kmalloc_array_node(sizeof(u64), page_cnt, GFP_ATOMIC,
  134. rdsibdev_to_node(rds_ibdev));
  135. if (!dma_pages) {
  136. ib_dma_unmap_sg(dev, sg, nents, DMA_BIDIRECTIONAL);
  137. return -ENOMEM;
  138. }
  139. page_cnt = 0;
  140. for (i = 0; i < sg_dma_len; ++i) {
  141. unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
  142. u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
  143. for (j = 0; j < dma_len; j += PAGE_SIZE)
  144. dma_pages[page_cnt++] =
  145. (dma_addr & PAGE_MASK) + j;
  146. }
  147. ret = ib_map_phys_fmr(fmr->fmr, dma_pages, page_cnt, io_addr);
  148. if (ret) {
  149. ib_dma_unmap_sg(dev, sg, nents, DMA_BIDIRECTIONAL);
  150. goto out;
  151. }
  152. /* Success - we successfully remapped the MR, so we can
  153. * safely tear down the old mapping.
  154. */
  155. rds_ib_teardown_mr(ibmr);
  156. ibmr->sg = scat;
  157. ibmr->sg_len = nents;
  158. ibmr->sg_dma_len = sg_dma_len;
  159. ibmr->remap_count++;
  160. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  161. rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
  162. else
  163. rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
  164. ret = 0;
  165. out:
  166. kfree(dma_pages);
  167. return ret;
  168. }
  169. struct rds_ib_mr *rds_ib_reg_fmr(struct rds_ib_device *rds_ibdev,
  170. struct scatterlist *sg,
  171. unsigned long nents,
  172. u32 *key)
  173. {
  174. struct rds_ib_mr *ibmr = NULL;
  175. struct rds_ib_fmr *fmr;
  176. int ret;
  177. ibmr = rds_ib_alloc_fmr(rds_ibdev, nents);
  178. if (IS_ERR(ibmr))
  179. return ibmr;
  180. ibmr->device = rds_ibdev;
  181. fmr = &ibmr->u.fmr;
  182. ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents);
  183. if (ret == 0)
  184. *key = fmr->fmr->rkey;
  185. else
  186. rds_ib_free_mr(ibmr, 0);
  187. return ibmr;
  188. }
  189. void rds_ib_unreg_fmr(struct list_head *list, unsigned int *nfreed,
  190. unsigned long *unpinned, unsigned int goal)
  191. {
  192. struct rds_ib_mr *ibmr, *next;
  193. struct rds_ib_fmr *fmr;
  194. LIST_HEAD(fmr_list);
  195. int ret = 0;
  196. unsigned int freed = *nfreed;
  197. /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
  198. list_for_each_entry(ibmr, list, unmap_list) {
  199. fmr = &ibmr->u.fmr;
  200. list_add(&fmr->fmr->list, &fmr_list);
  201. }
  202. ret = ib_unmap_fmr(&fmr_list);
  203. if (ret)
  204. pr_warn("RDS/IB: FMR invalidation failed (err=%d)\n", ret);
  205. /* Now we can destroy the DMA mapping and unpin any pages */
  206. list_for_each_entry_safe(ibmr, next, list, unmap_list) {
  207. fmr = &ibmr->u.fmr;
  208. *unpinned += ibmr->sg_len;
  209. __rds_ib_teardown_mr(ibmr);
  210. if (freed < goal ||
  211. ibmr->remap_count >= ibmr->pool->fmr_attr.max_maps) {
  212. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  213. rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
  214. else
  215. rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
  216. list_del(&ibmr->unmap_list);
  217. ib_dealloc_fmr(fmr->fmr);
  218. kfree(ibmr);
  219. freed++;
  220. }
  221. }
  222. *nfreed = freed;
  223. }
  224. void rds_ib_free_fmr_list(struct rds_ib_mr *ibmr)
  225. {
  226. struct rds_ib_mr_pool *pool = ibmr->pool;
  227. if (ibmr->remap_count >= pool->fmr_attr.max_maps)
  228. llist_add(&ibmr->llnode, &pool->drop_list);
  229. else
  230. llist_add(&ibmr->llnode, &pool->free_list);
  231. }