ib_frmr.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
  34. int npages)
  35. {
  36. struct rds_ib_mr_pool *pool;
  37. struct rds_ib_mr *ibmr = NULL;
  38. struct rds_ib_frmr *frmr;
  39. int err = 0;
  40. if (npages <= RDS_MR_8K_MSG_SIZE)
  41. pool = rds_ibdev->mr_8k_pool;
  42. else
  43. pool = rds_ibdev->mr_1m_pool;
  44. ibmr = rds_ib_try_reuse_ibmr(pool);
  45. if (ibmr)
  46. return ibmr;
  47. ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL,
  48. rdsibdev_to_node(rds_ibdev));
  49. if (!ibmr) {
  50. err = -ENOMEM;
  51. goto out_no_cigar;
  52. }
  53. frmr = &ibmr->u.frmr;
  54. frmr->mr = ib_alloc_mr(rds_ibdev->pd, IB_MR_TYPE_MEM_REG,
  55. pool->fmr_attr.max_pages);
  56. if (IS_ERR(frmr->mr)) {
  57. pr_warn("RDS/IB: %s failed to allocate MR", __func__);
  58. err = PTR_ERR(frmr->mr);
  59. goto out_no_cigar;
  60. }
  61. ibmr->pool = pool;
  62. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  63. rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
  64. else
  65. rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
  66. if (atomic_read(&pool->item_count) > pool->max_items_soft)
  67. pool->max_items_soft = pool->max_items;
  68. frmr->fr_state = FRMR_IS_FREE;
  69. return ibmr;
  70. out_no_cigar:
  71. kfree(ibmr);
  72. atomic_dec(&pool->item_count);
  73. return ERR_PTR(err);
  74. }
  75. static void rds_ib_free_frmr(struct rds_ib_mr *ibmr, bool drop)
  76. {
  77. struct rds_ib_mr_pool *pool = ibmr->pool;
  78. if (drop)
  79. llist_add(&ibmr->llnode, &pool->drop_list);
  80. else
  81. llist_add(&ibmr->llnode, &pool->free_list);
  82. atomic_add(ibmr->sg_len, &pool->free_pinned);
  83. atomic_inc(&pool->dirty_count);
  84. /* If we've pinned too many pages, request a flush */
  85. if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
  86. atomic_read(&pool->dirty_count) >= pool->max_items / 5)
  87. queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
  88. }
  89. static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
  90. {
  91. struct rds_ib_frmr *frmr = &ibmr->u.frmr;
  92. struct ib_reg_wr reg_wr;
  93. int ret, off = 0;
  94. while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
  95. atomic_inc(&ibmr->ic->i_fastreg_wrs);
  96. cpu_relax();
  97. }
  98. ret = ib_map_mr_sg_zbva(frmr->mr, ibmr->sg, ibmr->sg_len,
  99. &off, PAGE_SIZE);
  100. if (unlikely(ret != ibmr->sg_len))
  101. return ret < 0 ? ret : -EINVAL;
  102. /* Perform a WR for the fast_reg_mr. Each individual page
  103. * in the sg list is added to the fast reg page list and placed
  104. * inside the fast_reg_mr WR. The key used is a rolling 8bit
  105. * counter, which should guarantee uniqueness.
  106. */
  107. ib_update_fast_reg_key(frmr->mr, ibmr->remap_count++);
  108. frmr->fr_state = FRMR_IS_INUSE;
  109. memset(&reg_wr, 0, sizeof(reg_wr));
  110. reg_wr.wr.wr_id = (unsigned long)(void *)ibmr;
  111. reg_wr.wr.opcode = IB_WR_REG_MR;
  112. reg_wr.wr.num_sge = 0;
  113. reg_wr.mr = frmr->mr;
  114. reg_wr.key = frmr->mr->rkey;
  115. reg_wr.access = IB_ACCESS_LOCAL_WRITE |
  116. IB_ACCESS_REMOTE_READ |
  117. IB_ACCESS_REMOTE_WRITE;
  118. reg_wr.wr.send_flags = IB_SEND_SIGNALED;
  119. ret = ib_post_send(ibmr->ic->i_cm_id->qp, &reg_wr.wr, NULL);
  120. if (unlikely(ret)) {
  121. /* Failure here can be because of -ENOMEM as well */
  122. frmr->fr_state = FRMR_IS_STALE;
  123. atomic_inc(&ibmr->ic->i_fastreg_wrs);
  124. if (printk_ratelimit())
  125. pr_warn("RDS/IB: %s returned error(%d)\n",
  126. __func__, ret);
  127. }
  128. return ret;
  129. }
  130. static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev,
  131. struct rds_ib_mr_pool *pool,
  132. struct rds_ib_mr *ibmr,
  133. struct scatterlist *sg, unsigned int sg_len)
  134. {
  135. struct ib_device *dev = rds_ibdev->dev;
  136. struct rds_ib_frmr *frmr = &ibmr->u.frmr;
  137. int i;
  138. u32 len;
  139. int ret = 0;
  140. /* We want to teardown old ibmr values here and fill it up with
  141. * new sg values
  142. */
  143. rds_ib_teardown_mr(ibmr);
  144. ibmr->sg = sg;
  145. ibmr->sg_len = sg_len;
  146. ibmr->sg_dma_len = 0;
  147. frmr->sg_byte_len = 0;
  148. WARN_ON(ibmr->sg_dma_len);
  149. ibmr->sg_dma_len = ib_dma_map_sg(dev, ibmr->sg, ibmr->sg_len,
  150. DMA_BIDIRECTIONAL);
  151. if (unlikely(!ibmr->sg_dma_len)) {
  152. pr_warn("RDS/IB: %s failed!\n", __func__);
  153. return -EBUSY;
  154. }
  155. frmr->sg_byte_len = 0;
  156. frmr->dma_npages = 0;
  157. len = 0;
  158. ret = -EINVAL;
  159. for (i = 0; i < ibmr->sg_dma_len; ++i) {
  160. unsigned int dma_len = ib_sg_dma_len(dev, &ibmr->sg[i]);
  161. u64 dma_addr = ib_sg_dma_address(dev, &ibmr->sg[i]);
  162. frmr->sg_byte_len += dma_len;
  163. if (dma_addr & ~PAGE_MASK) {
  164. if (i > 0)
  165. goto out_unmap;
  166. else
  167. ++frmr->dma_npages;
  168. }
  169. if ((dma_addr + dma_len) & ~PAGE_MASK) {
  170. if (i < ibmr->sg_dma_len - 1)
  171. goto out_unmap;
  172. else
  173. ++frmr->dma_npages;
  174. }
  175. len += dma_len;
  176. }
  177. frmr->dma_npages += len >> PAGE_SHIFT;
  178. if (frmr->dma_npages > ibmr->pool->fmr_attr.max_pages) {
  179. ret = -EMSGSIZE;
  180. goto out_unmap;
  181. }
  182. ret = rds_ib_post_reg_frmr(ibmr);
  183. if (ret)
  184. goto out_unmap;
  185. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  186. rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
  187. else
  188. rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
  189. return ret;
  190. out_unmap:
  191. ib_dma_unmap_sg(rds_ibdev->dev, ibmr->sg, ibmr->sg_len,
  192. DMA_BIDIRECTIONAL);
  193. ibmr->sg_dma_len = 0;
  194. return ret;
  195. }
  196. static int rds_ib_post_inv(struct rds_ib_mr *ibmr)
  197. {
  198. struct ib_send_wr *s_wr;
  199. struct rds_ib_frmr *frmr = &ibmr->u.frmr;
  200. struct rdma_cm_id *i_cm_id = ibmr->ic->i_cm_id;
  201. int ret = -EINVAL;
  202. if (!i_cm_id || !i_cm_id->qp || !frmr->mr)
  203. goto out;
  204. if (frmr->fr_state != FRMR_IS_INUSE)
  205. goto out;
  206. while (atomic_dec_return(&ibmr->ic->i_fastunreg_wrs) <= 0) {
  207. atomic_inc(&ibmr->ic->i_fastunreg_wrs);
  208. cpu_relax();
  209. }
  210. frmr->fr_inv = true;
  211. s_wr = &frmr->fr_wr;
  212. memset(s_wr, 0, sizeof(*s_wr));
  213. s_wr->wr_id = (unsigned long)(void *)ibmr;
  214. s_wr->opcode = IB_WR_LOCAL_INV;
  215. s_wr->ex.invalidate_rkey = frmr->mr->rkey;
  216. s_wr->send_flags = IB_SEND_SIGNALED;
  217. ret = ib_post_send(i_cm_id->qp, s_wr, NULL);
  218. if (unlikely(ret)) {
  219. frmr->fr_state = FRMR_IS_STALE;
  220. frmr->fr_inv = false;
  221. atomic_inc(&ibmr->ic->i_fastunreg_wrs);
  222. pr_err("RDS/IB: %s returned error(%d)\n", __func__, ret);
  223. goto out;
  224. }
  225. out:
  226. return ret;
  227. }
  228. void rds_ib_mr_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc)
  229. {
  230. struct rds_ib_mr *ibmr = (void *)(unsigned long)wc->wr_id;
  231. struct rds_ib_frmr *frmr = &ibmr->u.frmr;
  232. if (wc->status != IB_WC_SUCCESS) {
  233. frmr->fr_state = FRMR_IS_STALE;
  234. if (rds_conn_up(ic->conn))
  235. rds_ib_conn_error(ic->conn,
  236. "frmr completion <%pI4,%pI4> status %u(%s), vendor_err 0x%x, disconnecting and reconnecting\n",
  237. &ic->conn->c_laddr,
  238. &ic->conn->c_faddr,
  239. wc->status,
  240. ib_wc_status_msg(wc->status),
  241. wc->vendor_err);
  242. }
  243. if (frmr->fr_inv) {
  244. frmr->fr_state = FRMR_IS_FREE;
  245. frmr->fr_inv = false;
  246. atomic_inc(&ic->i_fastreg_wrs);
  247. } else {
  248. atomic_inc(&ic->i_fastunreg_wrs);
  249. }
  250. }
  251. void rds_ib_unreg_frmr(struct list_head *list, unsigned int *nfreed,
  252. unsigned long *unpinned, unsigned int goal)
  253. {
  254. struct rds_ib_mr *ibmr, *next;
  255. struct rds_ib_frmr *frmr;
  256. int ret = 0;
  257. unsigned int freed = *nfreed;
  258. /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
  259. list_for_each_entry(ibmr, list, unmap_list) {
  260. if (ibmr->sg_dma_len)
  261. ret |= rds_ib_post_inv(ibmr);
  262. }
  263. if (ret)
  264. pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, ret);
  265. /* Now we can destroy the DMA mapping and unpin any pages */
  266. list_for_each_entry_safe(ibmr, next, list, unmap_list) {
  267. *unpinned += ibmr->sg_len;
  268. frmr = &ibmr->u.frmr;
  269. __rds_ib_teardown_mr(ibmr);
  270. if (freed < goal || frmr->fr_state == FRMR_IS_STALE) {
  271. /* Don't de-allocate if the MR is not free yet */
  272. if (frmr->fr_state == FRMR_IS_INUSE)
  273. continue;
  274. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  275. rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
  276. else
  277. rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
  278. list_del(&ibmr->unmap_list);
  279. if (frmr->mr)
  280. ib_dereg_mr(frmr->mr);
  281. kfree(ibmr);
  282. freed++;
  283. }
  284. }
  285. *nfreed = freed;
  286. }
  287. struct rds_ib_mr *rds_ib_reg_frmr(struct rds_ib_device *rds_ibdev,
  288. struct rds_ib_connection *ic,
  289. struct scatterlist *sg,
  290. unsigned long nents, u32 *key)
  291. {
  292. struct rds_ib_mr *ibmr = NULL;
  293. struct rds_ib_frmr *frmr;
  294. int ret;
  295. if (!ic) {
  296. /* TODO: Add FRWR support for RDS_GET_MR using proxy qp*/
  297. return ERR_PTR(-EOPNOTSUPP);
  298. }
  299. do {
  300. if (ibmr)
  301. rds_ib_free_frmr(ibmr, true);
  302. ibmr = rds_ib_alloc_frmr(rds_ibdev, nents);
  303. if (IS_ERR(ibmr))
  304. return ibmr;
  305. frmr = &ibmr->u.frmr;
  306. } while (frmr->fr_state != FRMR_IS_FREE);
  307. ibmr->ic = ic;
  308. ibmr->device = rds_ibdev;
  309. ret = rds_ib_map_frmr(rds_ibdev, ibmr->pool, ibmr, sg, nents);
  310. if (ret == 0) {
  311. *key = frmr->mr->rkey;
  312. } else {
  313. rds_ib_free_frmr(ibmr, false);
  314. ibmr = ERR_PTR(ret);
  315. }
  316. return ibmr;
  317. }
  318. void rds_ib_free_frmr_list(struct rds_ib_mr *ibmr)
  319. {
  320. struct rds_ib_mr_pool *pool = ibmr->pool;
  321. struct rds_ib_frmr *frmr = &ibmr->u.frmr;
  322. if (frmr->fr_state == FRMR_IS_STALE)
  323. llist_add(&ibmr->llnode, &pool->drop_list);
  324. else
  325. llist_add(&ibmr->llnode, &pool->free_list);
  326. }