iw_rdma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Copyright (c) 2006 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. */
  33. #include <linux/kernel.h>
  34. #include <linux/slab.h>
  35. #include <linux/ratelimit.h>
  36. #include "rds.h"
  37. #include "iw.h"
  38. /*
  39. * This is stored as mr->r_trans_private.
  40. */
  41. struct rds_iw_mr {
  42. struct rds_iw_device *device;
  43. struct rds_iw_mr_pool *pool;
  44. struct rdma_cm_id *cm_id;
  45. struct ib_mr *mr;
  46. struct ib_fast_reg_page_list *page_list;
  47. struct rds_iw_mapping mapping;
  48. unsigned char remap_count;
  49. };
  50. /*
  51. * Our own little MR pool
  52. */
  53. struct rds_iw_mr_pool {
  54. struct rds_iw_device *device; /* back ptr to the device that owns us */
  55. struct mutex flush_lock; /* serialize fmr invalidate */
  56. struct work_struct flush_worker; /* flush worker */
  57. spinlock_t list_lock; /* protect variables below */
  58. atomic_t item_count; /* total # of MRs */
  59. atomic_t dirty_count; /* # dirty of MRs */
  60. struct list_head dirty_list; /* dirty mappings */
  61. struct list_head clean_list; /* unused & unamapped MRs */
  62. atomic_t free_pinned; /* memory pinned by free MRs */
  63. unsigned long max_message_size; /* in pages */
  64. unsigned long max_items;
  65. unsigned long max_items_soft;
  66. unsigned long max_free_pinned;
  67. int max_pages;
  68. };
  69. static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all);
  70. static void rds_iw_mr_pool_flush_worker(struct work_struct *work);
  71. static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
  72. static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
  73. struct rds_iw_mr *ibmr,
  74. struct scatterlist *sg, unsigned int nents);
  75. static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
  76. static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
  77. struct list_head *unmap_list,
  78. struct list_head *kill_list,
  79. int *unpinned);
  80. static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
  81. static int rds_iw_get_device(struct rds_sock *rs, struct rds_iw_device **rds_iwdev, struct rdma_cm_id **cm_id)
  82. {
  83. struct rds_iw_device *iwdev;
  84. struct rds_iw_cm_id *i_cm_id;
  85. *rds_iwdev = NULL;
  86. *cm_id = NULL;
  87. list_for_each_entry(iwdev, &rds_iw_devices, list) {
  88. spin_lock_irq(&iwdev->spinlock);
  89. list_for_each_entry(i_cm_id, &iwdev->cm_id_list, list) {
  90. struct sockaddr_in *src_addr, *dst_addr;
  91. src_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.src_addr;
  92. dst_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.dst_addr;
  93. rdsdebug("local ipaddr = %x port %d, "
  94. "remote ipaddr = %x port %d"
  95. "..looking for %x port %d, "
  96. "remote ipaddr = %x port %d\n",
  97. src_addr->sin_addr.s_addr,
  98. src_addr->sin_port,
  99. dst_addr->sin_addr.s_addr,
  100. dst_addr->sin_port,
  101. rs->rs_bound_addr,
  102. rs->rs_bound_port,
  103. rs->rs_conn_addr,
  104. rs->rs_conn_port);
  105. #ifdef WORKING_TUPLE_DETECTION
  106. if (src_addr->sin_addr.s_addr == rs->rs_bound_addr &&
  107. src_addr->sin_port == rs->rs_bound_port &&
  108. dst_addr->sin_addr.s_addr == rs->rs_conn_addr &&
  109. dst_addr->sin_port == rs->rs_conn_port) {
  110. #else
  111. /* FIXME - needs to compare the local and remote
  112. * ipaddr/port tuple, but the ipaddr is the only
  113. * available information in the rds_sock (as the rest are
  114. * zero'ed. It doesn't appear to be properly populated
  115. * during connection setup...
  116. */
  117. if (src_addr->sin_addr.s_addr == rs->rs_bound_addr) {
  118. #endif
  119. spin_unlock_irq(&iwdev->spinlock);
  120. *rds_iwdev = iwdev;
  121. *cm_id = i_cm_id->cm_id;
  122. return 0;
  123. }
  124. }
  125. spin_unlock_irq(&iwdev->spinlock);
  126. }
  127. return 1;
  128. }
  129. static int rds_iw_add_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
  130. {
  131. struct rds_iw_cm_id *i_cm_id;
  132. i_cm_id = kmalloc(sizeof *i_cm_id, GFP_KERNEL);
  133. if (!i_cm_id)
  134. return -ENOMEM;
  135. i_cm_id->cm_id = cm_id;
  136. spin_lock_irq(&rds_iwdev->spinlock);
  137. list_add_tail(&i_cm_id->list, &rds_iwdev->cm_id_list);
  138. spin_unlock_irq(&rds_iwdev->spinlock);
  139. return 0;
  140. }
  141. static void rds_iw_remove_cm_id(struct rds_iw_device *rds_iwdev,
  142. struct rdma_cm_id *cm_id)
  143. {
  144. struct rds_iw_cm_id *i_cm_id;
  145. spin_lock_irq(&rds_iwdev->spinlock);
  146. list_for_each_entry(i_cm_id, &rds_iwdev->cm_id_list, list) {
  147. if (i_cm_id->cm_id == cm_id) {
  148. list_del(&i_cm_id->list);
  149. kfree(i_cm_id);
  150. break;
  151. }
  152. }
  153. spin_unlock_irq(&rds_iwdev->spinlock);
  154. }
  155. int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
  156. {
  157. struct sockaddr_in *src_addr, *dst_addr;
  158. struct rds_iw_device *rds_iwdev_old;
  159. struct rds_sock rs;
  160. struct rdma_cm_id *pcm_id;
  161. int rc;
  162. src_addr = (struct sockaddr_in *)&cm_id->route.addr.src_addr;
  163. dst_addr = (struct sockaddr_in *)&cm_id->route.addr.dst_addr;
  164. rs.rs_bound_addr = src_addr->sin_addr.s_addr;
  165. rs.rs_bound_port = src_addr->sin_port;
  166. rs.rs_conn_addr = dst_addr->sin_addr.s_addr;
  167. rs.rs_conn_port = dst_addr->sin_port;
  168. rc = rds_iw_get_device(&rs, &rds_iwdev_old, &pcm_id);
  169. if (rc)
  170. rds_iw_remove_cm_id(rds_iwdev, cm_id);
  171. return rds_iw_add_cm_id(rds_iwdev, cm_id);
  172. }
  173. void rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
  174. {
  175. struct rds_iw_connection *ic = conn->c_transport_data;
  176. /* conn was previously on the nodev_conns_list */
  177. spin_lock_irq(&iw_nodev_conns_lock);
  178. BUG_ON(list_empty(&iw_nodev_conns));
  179. BUG_ON(list_empty(&ic->iw_node));
  180. list_del(&ic->iw_node);
  181. spin_lock(&rds_iwdev->spinlock);
  182. list_add_tail(&ic->iw_node, &rds_iwdev->conn_list);
  183. spin_unlock(&rds_iwdev->spinlock);
  184. spin_unlock_irq(&iw_nodev_conns_lock);
  185. ic->rds_iwdev = rds_iwdev;
  186. }
  187. void rds_iw_remove_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
  188. {
  189. struct rds_iw_connection *ic = conn->c_transport_data;
  190. /* place conn on nodev_conns_list */
  191. spin_lock(&iw_nodev_conns_lock);
  192. spin_lock_irq(&rds_iwdev->spinlock);
  193. BUG_ON(list_empty(&ic->iw_node));
  194. list_del(&ic->iw_node);
  195. spin_unlock_irq(&rds_iwdev->spinlock);
  196. list_add_tail(&ic->iw_node, &iw_nodev_conns);
  197. spin_unlock(&iw_nodev_conns_lock);
  198. rds_iw_remove_cm_id(ic->rds_iwdev, ic->i_cm_id);
  199. ic->rds_iwdev = NULL;
  200. }
  201. void __rds_iw_destroy_conns(struct list_head *list, spinlock_t *list_lock)
  202. {
  203. struct rds_iw_connection *ic, *_ic;
  204. LIST_HEAD(tmp_list);
  205. /* avoid calling conn_destroy with irqs off */
  206. spin_lock_irq(list_lock);
  207. list_splice(list, &tmp_list);
  208. INIT_LIST_HEAD(list);
  209. spin_unlock_irq(list_lock);
  210. list_for_each_entry_safe(ic, _ic, &tmp_list, iw_node)
  211. rds_conn_destroy(ic->conn);
  212. }
  213. static void rds_iw_set_scatterlist(struct rds_iw_scatterlist *sg,
  214. struct scatterlist *list, unsigned int sg_len)
  215. {
  216. sg->list = list;
  217. sg->len = sg_len;
  218. sg->dma_len = 0;
  219. sg->dma_npages = 0;
  220. sg->bytes = 0;
  221. }
  222. static u64 *rds_iw_map_scatterlist(struct rds_iw_device *rds_iwdev,
  223. struct rds_iw_scatterlist *sg)
  224. {
  225. struct ib_device *dev = rds_iwdev->dev;
  226. u64 *dma_pages = NULL;
  227. int i, j, ret;
  228. WARN_ON(sg->dma_len);
  229. sg->dma_len = ib_dma_map_sg(dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
  230. if (unlikely(!sg->dma_len)) {
  231. printk(KERN_WARNING "RDS/IW: dma_map_sg failed!\n");
  232. return ERR_PTR(-EBUSY);
  233. }
  234. sg->bytes = 0;
  235. sg->dma_npages = 0;
  236. ret = -EINVAL;
  237. for (i = 0; i < sg->dma_len; ++i) {
  238. unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
  239. u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
  240. u64 end_addr;
  241. sg->bytes += dma_len;
  242. end_addr = dma_addr + dma_len;
  243. if (dma_addr & PAGE_MASK) {
  244. if (i > 0)
  245. goto out_unmap;
  246. dma_addr &= ~PAGE_MASK;
  247. }
  248. if (end_addr & PAGE_MASK) {
  249. if (i < sg->dma_len - 1)
  250. goto out_unmap;
  251. end_addr = (end_addr + PAGE_MASK) & ~PAGE_MASK;
  252. }
  253. sg->dma_npages += (end_addr - dma_addr) >> PAGE_SHIFT;
  254. }
  255. /* Now gather the dma addrs into one list */
  256. if (sg->dma_npages > fastreg_message_size)
  257. goto out_unmap;
  258. dma_pages = kmalloc(sizeof(u64) * sg->dma_npages, GFP_ATOMIC);
  259. if (!dma_pages) {
  260. ret = -ENOMEM;
  261. goto out_unmap;
  262. }
  263. for (i = j = 0; i < sg->dma_len; ++i) {
  264. unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
  265. u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
  266. u64 end_addr;
  267. end_addr = dma_addr + dma_len;
  268. dma_addr &= ~PAGE_MASK;
  269. for (; dma_addr < end_addr; dma_addr += PAGE_SIZE)
  270. dma_pages[j++] = dma_addr;
  271. BUG_ON(j > sg->dma_npages);
  272. }
  273. return dma_pages;
  274. out_unmap:
  275. ib_dma_unmap_sg(rds_iwdev->dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
  276. sg->dma_len = 0;
  277. kfree(dma_pages);
  278. return ERR_PTR(ret);
  279. }
  280. struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *rds_iwdev)
  281. {
  282. struct rds_iw_mr_pool *pool;
  283. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  284. if (!pool) {
  285. printk(KERN_WARNING "RDS/IW: rds_iw_create_mr_pool alloc error\n");
  286. return ERR_PTR(-ENOMEM);
  287. }
  288. pool->device = rds_iwdev;
  289. INIT_LIST_HEAD(&pool->dirty_list);
  290. INIT_LIST_HEAD(&pool->clean_list);
  291. mutex_init(&pool->flush_lock);
  292. spin_lock_init(&pool->list_lock);
  293. INIT_WORK(&pool->flush_worker, rds_iw_mr_pool_flush_worker);
  294. pool->max_message_size = fastreg_message_size;
  295. pool->max_items = fastreg_pool_size;
  296. pool->max_free_pinned = pool->max_items * pool->max_message_size / 4;
  297. pool->max_pages = fastreg_message_size;
  298. /* We never allow more than max_items MRs to be allocated.
  299. * When we exceed more than max_items_soft, we start freeing
  300. * items more aggressively.
  301. * Make sure that max_items > max_items_soft > max_items / 2
  302. */
  303. pool->max_items_soft = pool->max_items * 3 / 4;
  304. return pool;
  305. }
  306. void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo)
  307. {
  308. struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
  309. iinfo->rdma_mr_max = pool->max_items;
  310. iinfo->rdma_mr_size = pool->max_pages;
  311. }
  312. void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *pool)
  313. {
  314. flush_workqueue(rds_wq);
  315. rds_iw_flush_mr_pool(pool, 1);
  316. BUG_ON(atomic_read(&pool->item_count));
  317. BUG_ON(atomic_read(&pool->free_pinned));
  318. kfree(pool);
  319. }
  320. static inline struct rds_iw_mr *rds_iw_reuse_fmr(struct rds_iw_mr_pool *pool)
  321. {
  322. struct rds_iw_mr *ibmr = NULL;
  323. unsigned long flags;
  324. spin_lock_irqsave(&pool->list_lock, flags);
  325. if (!list_empty(&pool->clean_list)) {
  326. ibmr = list_entry(pool->clean_list.next, struct rds_iw_mr, mapping.m_list);
  327. list_del_init(&ibmr->mapping.m_list);
  328. }
  329. spin_unlock_irqrestore(&pool->list_lock, flags);
  330. return ibmr;
  331. }
  332. static struct rds_iw_mr *rds_iw_alloc_mr(struct rds_iw_device *rds_iwdev)
  333. {
  334. struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
  335. struct rds_iw_mr *ibmr = NULL;
  336. int err = 0, iter = 0;
  337. while (1) {
  338. ibmr = rds_iw_reuse_fmr(pool);
  339. if (ibmr)
  340. return ibmr;
  341. /* No clean MRs - now we have the choice of either
  342. * allocating a fresh MR up to the limit imposed by the
  343. * driver, or flush any dirty unused MRs.
  344. * We try to avoid stalling in the send path if possible,
  345. * so we allocate as long as we're allowed to.
  346. *
  347. * We're fussy with enforcing the FMR limit, though. If the driver
  348. * tells us we can't use more than N fmrs, we shouldn't start
  349. * arguing with it */
  350. if (atomic_inc_return(&pool->item_count) <= pool->max_items)
  351. break;
  352. atomic_dec(&pool->item_count);
  353. if (++iter > 2) {
  354. rds_iw_stats_inc(s_iw_rdma_mr_pool_depleted);
  355. return ERR_PTR(-EAGAIN);
  356. }
  357. /* We do have some empty MRs. Flush them out. */
  358. rds_iw_stats_inc(s_iw_rdma_mr_pool_wait);
  359. rds_iw_flush_mr_pool(pool, 0);
  360. }
  361. ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL);
  362. if (!ibmr) {
  363. err = -ENOMEM;
  364. goto out_no_cigar;
  365. }
  366. spin_lock_init(&ibmr->mapping.m_lock);
  367. INIT_LIST_HEAD(&ibmr->mapping.m_list);
  368. ibmr->mapping.m_mr = ibmr;
  369. err = rds_iw_init_fastreg(pool, ibmr);
  370. if (err)
  371. goto out_no_cigar;
  372. rds_iw_stats_inc(s_iw_rdma_mr_alloc);
  373. return ibmr;
  374. out_no_cigar:
  375. if (ibmr) {
  376. rds_iw_destroy_fastreg(pool, ibmr);
  377. kfree(ibmr);
  378. }
  379. atomic_dec(&pool->item_count);
  380. return ERR_PTR(err);
  381. }
  382. void rds_iw_sync_mr(void *trans_private, int direction)
  383. {
  384. struct rds_iw_mr *ibmr = trans_private;
  385. struct rds_iw_device *rds_iwdev = ibmr->device;
  386. switch (direction) {
  387. case DMA_FROM_DEVICE:
  388. ib_dma_sync_sg_for_cpu(rds_iwdev->dev, ibmr->mapping.m_sg.list,
  389. ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
  390. break;
  391. case DMA_TO_DEVICE:
  392. ib_dma_sync_sg_for_device(rds_iwdev->dev, ibmr->mapping.m_sg.list,
  393. ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
  394. break;
  395. }
  396. }
  397. static inline unsigned int rds_iw_flush_goal(struct rds_iw_mr_pool *pool, int free_all)
  398. {
  399. unsigned int item_count;
  400. item_count = atomic_read(&pool->item_count);
  401. if (free_all)
  402. return item_count;
  403. return 0;
  404. }
  405. /*
  406. * Flush our pool of MRs.
  407. * At a minimum, all currently unused MRs are unmapped.
  408. * If the number of MRs allocated exceeds the limit, we also try
  409. * to free as many MRs as needed to get back to this limit.
  410. */
  411. static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all)
  412. {
  413. struct rds_iw_mr *ibmr, *next;
  414. LIST_HEAD(unmap_list);
  415. LIST_HEAD(kill_list);
  416. unsigned long flags;
  417. unsigned int nfreed = 0, ncleaned = 0, unpinned = 0, free_goal;
  418. int ret = 0;
  419. rds_iw_stats_inc(s_iw_rdma_mr_pool_flush);
  420. mutex_lock(&pool->flush_lock);
  421. spin_lock_irqsave(&pool->list_lock, flags);
  422. /* Get the list of all mappings to be destroyed */
  423. list_splice_init(&pool->dirty_list, &unmap_list);
  424. if (free_all)
  425. list_splice_init(&pool->clean_list, &kill_list);
  426. spin_unlock_irqrestore(&pool->list_lock, flags);
  427. free_goal = rds_iw_flush_goal(pool, free_all);
  428. /* Batched invalidate of dirty MRs.
  429. * For FMR based MRs, the mappings on the unmap list are
  430. * actually members of an ibmr (ibmr->mapping). They either
  431. * migrate to the kill_list, or have been cleaned and should be
  432. * moved to the clean_list.
  433. * For fastregs, they will be dynamically allocated, and
  434. * will be destroyed by the unmap function.
  435. */
  436. if (!list_empty(&unmap_list)) {
  437. ncleaned = rds_iw_unmap_fastreg_list(pool, &unmap_list,
  438. &kill_list, &unpinned);
  439. /* If we've been asked to destroy all MRs, move those
  440. * that were simply cleaned to the kill list */
  441. if (free_all)
  442. list_splice_init(&unmap_list, &kill_list);
  443. }
  444. /* Destroy any MRs that are past their best before date */
  445. list_for_each_entry_safe(ibmr, next, &kill_list, mapping.m_list) {
  446. rds_iw_stats_inc(s_iw_rdma_mr_free);
  447. list_del(&ibmr->mapping.m_list);
  448. rds_iw_destroy_fastreg(pool, ibmr);
  449. kfree(ibmr);
  450. nfreed++;
  451. }
  452. /* Anything that remains are laundered ibmrs, which we can add
  453. * back to the clean list. */
  454. if (!list_empty(&unmap_list)) {
  455. spin_lock_irqsave(&pool->list_lock, flags);
  456. list_splice(&unmap_list, &pool->clean_list);
  457. spin_unlock_irqrestore(&pool->list_lock, flags);
  458. }
  459. atomic_sub(unpinned, &pool->free_pinned);
  460. atomic_sub(ncleaned, &pool->dirty_count);
  461. atomic_sub(nfreed, &pool->item_count);
  462. mutex_unlock(&pool->flush_lock);
  463. return ret;
  464. }
  465. static void rds_iw_mr_pool_flush_worker(struct work_struct *work)
  466. {
  467. struct rds_iw_mr_pool *pool = container_of(work, struct rds_iw_mr_pool, flush_worker);
  468. rds_iw_flush_mr_pool(pool, 0);
  469. }
  470. void rds_iw_free_mr(void *trans_private, int invalidate)
  471. {
  472. struct rds_iw_mr *ibmr = trans_private;
  473. struct rds_iw_mr_pool *pool = ibmr->device->mr_pool;
  474. rdsdebug("RDS/IW: free_mr nents %u\n", ibmr->mapping.m_sg.len);
  475. if (!pool)
  476. return;
  477. /* Return it to the pool's free list */
  478. rds_iw_free_fastreg(pool, ibmr);
  479. /* If we've pinned too many pages, request a flush */
  480. if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
  481. atomic_read(&pool->dirty_count) >= pool->max_items / 10)
  482. queue_work(rds_wq, &pool->flush_worker);
  483. if (invalidate) {
  484. if (likely(!in_interrupt())) {
  485. rds_iw_flush_mr_pool(pool, 0);
  486. } else {
  487. /* We get here if the user created a MR marked
  488. * as use_once and invalidate at the same time. */
  489. queue_work(rds_wq, &pool->flush_worker);
  490. }
  491. }
  492. }
  493. void rds_iw_flush_mrs(void)
  494. {
  495. struct rds_iw_device *rds_iwdev;
  496. list_for_each_entry(rds_iwdev, &rds_iw_devices, list) {
  497. struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
  498. if (pool)
  499. rds_iw_flush_mr_pool(pool, 0);
  500. }
  501. }
  502. void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
  503. struct rds_sock *rs, u32 *key_ret)
  504. {
  505. struct rds_iw_device *rds_iwdev;
  506. struct rds_iw_mr *ibmr = NULL;
  507. struct rdma_cm_id *cm_id;
  508. int ret;
  509. ret = rds_iw_get_device(rs, &rds_iwdev, &cm_id);
  510. if (ret || !cm_id) {
  511. ret = -ENODEV;
  512. goto out;
  513. }
  514. if (!rds_iwdev->mr_pool) {
  515. ret = -ENODEV;
  516. goto out;
  517. }
  518. ibmr = rds_iw_alloc_mr(rds_iwdev);
  519. if (IS_ERR(ibmr))
  520. return ibmr;
  521. ibmr->cm_id = cm_id;
  522. ibmr->device = rds_iwdev;
  523. ret = rds_iw_map_fastreg(rds_iwdev->mr_pool, ibmr, sg, nents);
  524. if (ret == 0)
  525. *key_ret = ibmr->mr->rkey;
  526. else
  527. printk(KERN_WARNING "RDS/IW: failed to map mr (errno=%d)\n", ret);
  528. out:
  529. if (ret) {
  530. if (ibmr)
  531. rds_iw_free_mr(ibmr, 0);
  532. ibmr = ERR_PTR(ret);
  533. }
  534. return ibmr;
  535. }
  536. /*
  537. * iWARP fastreg handling
  538. *
  539. * The life cycle of a fastreg registration is a bit different from
  540. * FMRs.
  541. * The idea behind fastreg is to have one MR, to which we bind different
  542. * mappings over time. To avoid stalling on the expensive map and invalidate
  543. * operations, these operations are pipelined on the same send queue on
  544. * which we want to send the message containing the r_key.
  545. *
  546. * This creates a bit of a problem for us, as we do not have the destination
  547. * IP in GET_MR, so the connection must be setup prior to the GET_MR call for
  548. * RDMA to be correctly setup. If a fastreg request is present, rds_iw_xmit
  549. * will try to queue a LOCAL_INV (if needed) and a FAST_REG_MR work request
  550. * before queuing the SEND. When completions for these arrive, they are
  551. * dispatched to the MR has a bit set showing that RDMa can be performed.
  552. *
  553. * There is another interesting aspect that's related to invalidation.
  554. * The application can request that a mapping is invalidated in FREE_MR.
  555. * The expectation there is that this invalidation step includes ALL
  556. * PREVIOUSLY FREED MRs.
  557. */
  558. static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool,
  559. struct rds_iw_mr *ibmr)
  560. {
  561. struct rds_iw_device *rds_iwdev = pool->device;
  562. struct ib_fast_reg_page_list *page_list = NULL;
  563. struct ib_mr *mr;
  564. int err;
  565. mr = ib_alloc_fast_reg_mr(rds_iwdev->pd, pool->max_message_size);
  566. if (IS_ERR(mr)) {
  567. err = PTR_ERR(mr);
  568. printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_mr failed (err=%d)\n", err);
  569. return err;
  570. }
  571. /* FIXME - this is overkill, but mapping->m_sg.dma_len/mapping->m_sg.dma_npages
  572. * is not filled in.
  573. */
  574. page_list = ib_alloc_fast_reg_page_list(rds_iwdev->dev, pool->max_message_size);
  575. if (IS_ERR(page_list)) {
  576. err = PTR_ERR(page_list);
  577. printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed (err=%d)\n", err);
  578. ib_dereg_mr(mr);
  579. return err;
  580. }
  581. ibmr->page_list = page_list;
  582. ibmr->mr = mr;
  583. return 0;
  584. }
  585. static int rds_iw_rdma_build_fastreg(struct rds_iw_mapping *mapping)
  586. {
  587. struct rds_iw_mr *ibmr = mapping->m_mr;
  588. struct ib_send_wr f_wr, *failed_wr;
  589. int ret;
  590. /*
  591. * Perform a WR for the fast_reg_mr. Each individual page
  592. * in the sg list is added to the fast reg page list and placed
  593. * inside the fast_reg_mr WR. The key used is a rolling 8bit
  594. * counter, which should guarantee uniqueness.
  595. */
  596. ib_update_fast_reg_key(ibmr->mr, ibmr->remap_count++);
  597. mapping->m_rkey = ibmr->mr->rkey;
  598. memset(&f_wr, 0, sizeof(f_wr));
  599. f_wr.wr_id = RDS_IW_FAST_REG_WR_ID;
  600. f_wr.opcode = IB_WR_FAST_REG_MR;
  601. f_wr.wr.fast_reg.length = mapping->m_sg.bytes;
  602. f_wr.wr.fast_reg.rkey = mapping->m_rkey;
  603. f_wr.wr.fast_reg.page_list = ibmr->page_list;
  604. f_wr.wr.fast_reg.page_list_len = mapping->m_sg.dma_len;
  605. f_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  606. f_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE |
  607. IB_ACCESS_REMOTE_READ |
  608. IB_ACCESS_REMOTE_WRITE;
  609. f_wr.wr.fast_reg.iova_start = 0;
  610. f_wr.send_flags = IB_SEND_SIGNALED;
  611. failed_wr = &f_wr;
  612. ret = ib_post_send(ibmr->cm_id->qp, &f_wr, &failed_wr);
  613. BUG_ON(failed_wr != &f_wr);
  614. if (ret)
  615. printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
  616. __func__, __LINE__, ret);
  617. return ret;
  618. }
  619. static int rds_iw_rdma_fastreg_inv(struct rds_iw_mr *ibmr)
  620. {
  621. struct ib_send_wr s_wr, *failed_wr;
  622. int ret = 0;
  623. if (!ibmr->cm_id->qp || !ibmr->mr)
  624. goto out;
  625. memset(&s_wr, 0, sizeof(s_wr));
  626. s_wr.wr_id = RDS_IW_LOCAL_INV_WR_ID;
  627. s_wr.opcode = IB_WR_LOCAL_INV;
  628. s_wr.ex.invalidate_rkey = ibmr->mr->rkey;
  629. s_wr.send_flags = IB_SEND_SIGNALED;
  630. failed_wr = &s_wr;
  631. ret = ib_post_send(ibmr->cm_id->qp, &s_wr, &failed_wr);
  632. if (ret) {
  633. printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
  634. __func__, __LINE__, ret);
  635. goto out;
  636. }
  637. out:
  638. return ret;
  639. }
  640. static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
  641. struct rds_iw_mr *ibmr,
  642. struct scatterlist *sg,
  643. unsigned int sg_len)
  644. {
  645. struct rds_iw_device *rds_iwdev = pool->device;
  646. struct rds_iw_mapping *mapping = &ibmr->mapping;
  647. u64 *dma_pages;
  648. int i, ret = 0;
  649. rds_iw_set_scatterlist(&mapping->m_sg, sg, sg_len);
  650. dma_pages = rds_iw_map_scatterlist(rds_iwdev, &mapping->m_sg);
  651. if (IS_ERR(dma_pages)) {
  652. ret = PTR_ERR(dma_pages);
  653. dma_pages = NULL;
  654. goto out;
  655. }
  656. if (mapping->m_sg.dma_len > pool->max_message_size) {
  657. ret = -EMSGSIZE;
  658. goto out;
  659. }
  660. for (i = 0; i < mapping->m_sg.dma_npages; ++i)
  661. ibmr->page_list->page_list[i] = dma_pages[i];
  662. ret = rds_iw_rdma_build_fastreg(mapping);
  663. if (ret)
  664. goto out;
  665. rds_iw_stats_inc(s_iw_rdma_mr_used);
  666. out:
  667. kfree(dma_pages);
  668. return ret;
  669. }
  670. /*
  671. * "Free" a fastreg MR.
  672. */
  673. static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool,
  674. struct rds_iw_mr *ibmr)
  675. {
  676. unsigned long flags;
  677. int ret;
  678. if (!ibmr->mapping.m_sg.dma_len)
  679. return;
  680. ret = rds_iw_rdma_fastreg_inv(ibmr);
  681. if (ret)
  682. return;
  683. /* Try to post the LOCAL_INV WR to the queue. */
  684. spin_lock_irqsave(&pool->list_lock, flags);
  685. list_add_tail(&ibmr->mapping.m_list, &pool->dirty_list);
  686. atomic_add(ibmr->mapping.m_sg.len, &pool->free_pinned);
  687. atomic_inc(&pool->dirty_count);
  688. spin_unlock_irqrestore(&pool->list_lock, flags);
  689. }
  690. static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
  691. struct list_head *unmap_list,
  692. struct list_head *kill_list,
  693. int *unpinned)
  694. {
  695. struct rds_iw_mapping *mapping, *next;
  696. unsigned int ncleaned = 0;
  697. LIST_HEAD(laundered);
  698. /* Batched invalidation of fastreg MRs.
  699. * Why do we do it this way, even though we could pipeline unmap
  700. * and remap? The reason is the application semantics - when the
  701. * application requests an invalidation of MRs, it expects all
  702. * previously released R_Keys to become invalid.
  703. *
  704. * If we implement MR reuse naively, we risk memory corruption
  705. * (this has actually been observed). So the default behavior
  706. * requires that a MR goes through an explicit unmap operation before
  707. * we can reuse it again.
  708. *
  709. * We could probably improve on this a little, by allowing immediate
  710. * reuse of a MR on the same socket (eg you could add small
  711. * cache of unused MRs to strct rds_socket - GET_MR could grab one
  712. * of these without requiring an explicit invalidate).
  713. */
  714. while (!list_empty(unmap_list)) {
  715. unsigned long flags;
  716. spin_lock_irqsave(&pool->list_lock, flags);
  717. list_for_each_entry_safe(mapping, next, unmap_list, m_list) {
  718. *unpinned += mapping->m_sg.len;
  719. list_move(&mapping->m_list, &laundered);
  720. ncleaned++;
  721. }
  722. spin_unlock_irqrestore(&pool->list_lock, flags);
  723. }
  724. /* Move all laundered mappings back to the unmap list.
  725. * We do not kill any WRs right now - it doesn't seem the
  726. * fastreg API has a max_remap limit. */
  727. list_splice_init(&laundered, unmap_list);
  728. return ncleaned;
  729. }
  730. static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool,
  731. struct rds_iw_mr *ibmr)
  732. {
  733. if (ibmr->page_list)
  734. ib_free_fast_reg_page_list(ibmr->page_list);
  735. if (ibmr->mr)
  736. ib_dereg_mr(ibmr->mr);
  737. }