ib_frmr.c 9.8 KB

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