iw_rdma.c 24 KB

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