ib_rdma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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/rculist.h>
  36. #include <linux/llist.h>
  37. #include "rds.h"
  38. #include "ib.h"
  39. static DEFINE_PER_CPU(unsigned long, clean_list_grace);
  40. #define CLEAN_LIST_BUSY_BIT 0
  41. /*
  42. * This is stored as mr->r_trans_private.
  43. */
  44. struct rds_ib_mr {
  45. struct rds_ib_device *device;
  46. struct rds_ib_mr_pool *pool;
  47. struct ib_fmr *fmr;
  48. struct llist_node llnode;
  49. /* unmap_list is for freeing */
  50. struct list_head unmap_list;
  51. unsigned int remap_count;
  52. struct scatterlist *sg;
  53. unsigned int sg_len;
  54. u64 *dma;
  55. int sg_dma_len;
  56. };
  57. /*
  58. * Our own little FMR pool
  59. */
  60. struct rds_ib_mr_pool {
  61. unsigned int pool_type;
  62. struct mutex flush_lock; /* serialize fmr invalidate */
  63. struct delayed_work flush_worker; /* flush worker */
  64. atomic_t item_count; /* total # of MRs */
  65. atomic_t dirty_count; /* # dirty of MRs */
  66. struct llist_head drop_list; /* MRs that have reached their max_maps limit */
  67. struct llist_head free_list; /* unused MRs */
  68. struct llist_head clean_list; /* global unused & unamapped MRs */
  69. wait_queue_head_t flush_wait;
  70. atomic_t free_pinned; /* memory pinned by free MRs */
  71. unsigned long max_items;
  72. unsigned long max_items_soft;
  73. unsigned long max_free_pinned;
  74. struct ib_fmr_attr fmr_attr;
  75. };
  76. static struct workqueue_struct *rds_ib_fmr_wq;
  77. int rds_ib_fmr_init(void)
  78. {
  79. rds_ib_fmr_wq = create_workqueue("rds_fmr_flushd");
  80. if (!rds_ib_fmr_wq)
  81. return -ENOMEM;
  82. return 0;
  83. }
  84. /* By the time this is called all the IB devices should have been torn down and
  85. * had their pools freed. As each pool is freed its work struct is waited on,
  86. * so the pool flushing work queue should be idle by the time we get here.
  87. */
  88. void rds_ib_fmr_exit(void)
  89. {
  90. destroy_workqueue(rds_ib_fmr_wq);
  91. }
  92. static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool, int free_all, struct rds_ib_mr **);
  93. static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr);
  94. static void rds_ib_mr_pool_flush_worker(struct work_struct *work);
  95. static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
  96. {
  97. struct rds_ib_device *rds_ibdev;
  98. struct rds_ib_ipaddr *i_ipaddr;
  99. rcu_read_lock();
  100. list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) {
  101. list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
  102. if (i_ipaddr->ipaddr == ipaddr) {
  103. atomic_inc(&rds_ibdev->refcount);
  104. rcu_read_unlock();
  105. return rds_ibdev;
  106. }
  107. }
  108. }
  109. rcu_read_unlock();
  110. return NULL;
  111. }
  112. static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
  113. {
  114. struct rds_ib_ipaddr *i_ipaddr;
  115. i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
  116. if (!i_ipaddr)
  117. return -ENOMEM;
  118. i_ipaddr->ipaddr = ipaddr;
  119. spin_lock_irq(&rds_ibdev->spinlock);
  120. list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
  121. spin_unlock_irq(&rds_ibdev->spinlock);
  122. return 0;
  123. }
  124. static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
  125. {
  126. struct rds_ib_ipaddr *i_ipaddr;
  127. struct rds_ib_ipaddr *to_free = NULL;
  128. spin_lock_irq(&rds_ibdev->spinlock);
  129. list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
  130. if (i_ipaddr->ipaddr == ipaddr) {
  131. list_del_rcu(&i_ipaddr->list);
  132. to_free = i_ipaddr;
  133. break;
  134. }
  135. }
  136. spin_unlock_irq(&rds_ibdev->spinlock);
  137. if (to_free)
  138. kfree_rcu(to_free, rcu);
  139. }
  140. int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
  141. {
  142. struct rds_ib_device *rds_ibdev_old;
  143. rds_ibdev_old = rds_ib_get_device(ipaddr);
  144. if (!rds_ibdev_old)
  145. return rds_ib_add_ipaddr(rds_ibdev, ipaddr);
  146. if (rds_ibdev_old != rds_ibdev) {
  147. rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr);
  148. rds_ib_dev_put(rds_ibdev_old);
  149. return rds_ib_add_ipaddr(rds_ibdev, ipaddr);
  150. }
  151. rds_ib_dev_put(rds_ibdev_old);
  152. return 0;
  153. }
  154. void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
  155. {
  156. struct rds_ib_connection *ic = conn->c_transport_data;
  157. /* conn was previously on the nodev_conns_list */
  158. spin_lock_irq(&ib_nodev_conns_lock);
  159. BUG_ON(list_empty(&ib_nodev_conns));
  160. BUG_ON(list_empty(&ic->ib_node));
  161. list_del(&ic->ib_node);
  162. spin_lock(&rds_ibdev->spinlock);
  163. list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
  164. spin_unlock(&rds_ibdev->spinlock);
  165. spin_unlock_irq(&ib_nodev_conns_lock);
  166. ic->rds_ibdev = rds_ibdev;
  167. atomic_inc(&rds_ibdev->refcount);
  168. }
  169. void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
  170. {
  171. struct rds_ib_connection *ic = conn->c_transport_data;
  172. /* place conn on nodev_conns_list */
  173. spin_lock(&ib_nodev_conns_lock);
  174. spin_lock_irq(&rds_ibdev->spinlock);
  175. BUG_ON(list_empty(&ic->ib_node));
  176. list_del(&ic->ib_node);
  177. spin_unlock_irq(&rds_ibdev->spinlock);
  178. list_add_tail(&ic->ib_node, &ib_nodev_conns);
  179. spin_unlock(&ib_nodev_conns_lock);
  180. ic->rds_ibdev = NULL;
  181. rds_ib_dev_put(rds_ibdev);
  182. }
  183. void rds_ib_destroy_nodev_conns(void)
  184. {
  185. struct rds_ib_connection *ic, *_ic;
  186. LIST_HEAD(tmp_list);
  187. /* avoid calling conn_destroy with irqs off */
  188. spin_lock_irq(&ib_nodev_conns_lock);
  189. list_splice(&ib_nodev_conns, &tmp_list);
  190. spin_unlock_irq(&ib_nodev_conns_lock);
  191. list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
  192. rds_conn_destroy(ic->conn);
  193. }
  194. struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev,
  195. int pool_type)
  196. {
  197. struct rds_ib_mr_pool *pool;
  198. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  199. if (!pool)
  200. return ERR_PTR(-ENOMEM);
  201. pool->pool_type = pool_type;
  202. init_llist_head(&pool->free_list);
  203. init_llist_head(&pool->drop_list);
  204. init_llist_head(&pool->clean_list);
  205. mutex_init(&pool->flush_lock);
  206. init_waitqueue_head(&pool->flush_wait);
  207. INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
  208. if (pool_type == RDS_IB_MR_1M_POOL) {
  209. /* +1 allows for unaligned MRs */
  210. pool->fmr_attr.max_pages = RDS_FMR_1M_MSG_SIZE + 1;
  211. pool->max_items = RDS_FMR_1M_POOL_SIZE;
  212. } else {
  213. /* pool_type == RDS_IB_MR_8K_POOL */
  214. pool->fmr_attr.max_pages = RDS_FMR_8K_MSG_SIZE + 1;
  215. pool->max_items = RDS_FMR_8K_POOL_SIZE;
  216. }
  217. pool->max_free_pinned = pool->max_items * pool->fmr_attr.max_pages / 4;
  218. pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps;
  219. pool->fmr_attr.page_shift = PAGE_SHIFT;
  220. pool->max_items_soft = rds_ibdev->max_fmrs * 3 / 4;
  221. return pool;
  222. }
  223. void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
  224. {
  225. struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
  226. iinfo->rdma_mr_max = pool_1m->max_items;
  227. iinfo->rdma_mr_size = pool_1m->fmr_attr.max_pages;
  228. }
  229. void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
  230. {
  231. cancel_delayed_work_sync(&pool->flush_worker);
  232. rds_ib_flush_mr_pool(pool, 1, NULL);
  233. WARN_ON(atomic_read(&pool->item_count));
  234. WARN_ON(atomic_read(&pool->free_pinned));
  235. kfree(pool);
  236. }
  237. static inline struct rds_ib_mr *rds_ib_reuse_fmr(struct rds_ib_mr_pool *pool)
  238. {
  239. struct rds_ib_mr *ibmr = NULL;
  240. struct llist_node *ret;
  241. unsigned long *flag;
  242. preempt_disable();
  243. flag = this_cpu_ptr(&clean_list_grace);
  244. set_bit(CLEAN_LIST_BUSY_BIT, flag);
  245. ret = llist_del_first(&pool->clean_list);
  246. if (ret)
  247. ibmr = llist_entry(ret, struct rds_ib_mr, llnode);
  248. clear_bit(CLEAN_LIST_BUSY_BIT, flag);
  249. preempt_enable();
  250. return ibmr;
  251. }
  252. static inline void wait_clean_list_grace(void)
  253. {
  254. int cpu;
  255. unsigned long *flag;
  256. for_each_online_cpu(cpu) {
  257. flag = &per_cpu(clean_list_grace, cpu);
  258. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  259. cpu_relax();
  260. }
  261. }
  262. static struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev,
  263. int npages)
  264. {
  265. struct rds_ib_mr_pool *pool;
  266. struct rds_ib_mr *ibmr = NULL;
  267. int err = 0, iter = 0;
  268. if (npages <= RDS_FMR_8K_MSG_SIZE)
  269. pool = rds_ibdev->mr_8k_pool;
  270. else
  271. pool = rds_ibdev->mr_1m_pool;
  272. if (atomic_read(&pool->dirty_count) >= pool->max_items / 10)
  273. queue_delayed_work(rds_ib_fmr_wq, &pool->flush_worker, 10);
  274. /* Switch pools if one of the pool is reaching upper limit */
  275. if (atomic_read(&pool->dirty_count) >= pool->max_items * 9 / 10) {
  276. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  277. pool = rds_ibdev->mr_1m_pool;
  278. else
  279. pool = rds_ibdev->mr_8k_pool;
  280. }
  281. while (1) {
  282. ibmr = rds_ib_reuse_fmr(pool);
  283. if (ibmr)
  284. return ibmr;
  285. /* No clean MRs - now we have the choice of either
  286. * allocating a fresh MR up to the limit imposed by the
  287. * driver, or flush any dirty unused MRs.
  288. * We try to avoid stalling in the send path if possible,
  289. * so we allocate as long as we're allowed to.
  290. *
  291. * We're fussy with enforcing the FMR limit, though. If the driver
  292. * tells us we can't use more than N fmrs, we shouldn't start
  293. * arguing with it */
  294. if (atomic_inc_return(&pool->item_count) <= pool->max_items)
  295. break;
  296. atomic_dec(&pool->item_count);
  297. if (++iter > 2) {
  298. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  299. rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted);
  300. else
  301. rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted);
  302. return ERR_PTR(-EAGAIN);
  303. }
  304. /* We do have some empty MRs. Flush them out. */
  305. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  306. rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait);
  307. else
  308. rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait);
  309. rds_ib_flush_mr_pool(pool, 0, &ibmr);
  310. if (ibmr)
  311. return ibmr;
  312. }
  313. ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL, rdsibdev_to_node(rds_ibdev));
  314. if (!ibmr) {
  315. err = -ENOMEM;
  316. goto out_no_cigar;
  317. }
  318. ibmr->fmr = ib_alloc_fmr(rds_ibdev->pd,
  319. (IB_ACCESS_LOCAL_WRITE |
  320. IB_ACCESS_REMOTE_READ |
  321. IB_ACCESS_REMOTE_WRITE|
  322. IB_ACCESS_REMOTE_ATOMIC),
  323. &pool->fmr_attr);
  324. if (IS_ERR(ibmr->fmr)) {
  325. err = PTR_ERR(ibmr->fmr);
  326. ibmr->fmr = NULL;
  327. printk(KERN_WARNING "RDS/IB: ib_alloc_fmr failed (err=%d)\n", err);
  328. goto out_no_cigar;
  329. }
  330. ibmr->pool = pool;
  331. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  332. rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
  333. else
  334. rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
  335. return ibmr;
  336. out_no_cigar:
  337. if (ibmr) {
  338. if (ibmr->fmr)
  339. ib_dealloc_fmr(ibmr->fmr);
  340. kfree(ibmr);
  341. }
  342. atomic_dec(&pool->item_count);
  343. return ERR_PTR(err);
  344. }
  345. static int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr,
  346. struct scatterlist *sg, unsigned int nents)
  347. {
  348. struct ib_device *dev = rds_ibdev->dev;
  349. struct scatterlist *scat = sg;
  350. u64 io_addr = 0;
  351. u64 *dma_pages;
  352. u32 len;
  353. int page_cnt, sg_dma_len;
  354. int i, j;
  355. int ret;
  356. sg_dma_len = ib_dma_map_sg(dev, sg, nents,
  357. DMA_BIDIRECTIONAL);
  358. if (unlikely(!sg_dma_len)) {
  359. printk(KERN_WARNING "RDS/IB: dma_map_sg failed!\n");
  360. return -EBUSY;
  361. }
  362. len = 0;
  363. page_cnt = 0;
  364. for (i = 0; i < sg_dma_len; ++i) {
  365. unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
  366. u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
  367. if (dma_addr & ~PAGE_MASK) {
  368. if (i > 0)
  369. return -EINVAL;
  370. else
  371. ++page_cnt;
  372. }
  373. if ((dma_addr + dma_len) & ~PAGE_MASK) {
  374. if (i < sg_dma_len - 1)
  375. return -EINVAL;
  376. else
  377. ++page_cnt;
  378. }
  379. len += dma_len;
  380. }
  381. page_cnt += len >> PAGE_SHIFT;
  382. if (page_cnt > ibmr->pool->fmr_attr.max_pages)
  383. return -EINVAL;
  384. dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC,
  385. rdsibdev_to_node(rds_ibdev));
  386. if (!dma_pages)
  387. return -ENOMEM;
  388. page_cnt = 0;
  389. for (i = 0; i < sg_dma_len; ++i) {
  390. unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
  391. u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
  392. for (j = 0; j < dma_len; j += PAGE_SIZE)
  393. dma_pages[page_cnt++] =
  394. (dma_addr & PAGE_MASK) + j;
  395. }
  396. ret = ib_map_phys_fmr(ibmr->fmr,
  397. dma_pages, page_cnt, io_addr);
  398. if (ret)
  399. goto out;
  400. /* Success - we successfully remapped the MR, so we can
  401. * safely tear down the old mapping. */
  402. rds_ib_teardown_mr(ibmr);
  403. ibmr->sg = scat;
  404. ibmr->sg_len = nents;
  405. ibmr->sg_dma_len = sg_dma_len;
  406. ibmr->remap_count++;
  407. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  408. rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
  409. else
  410. rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
  411. ret = 0;
  412. out:
  413. kfree(dma_pages);
  414. return ret;
  415. }
  416. void rds_ib_sync_mr(void *trans_private, int direction)
  417. {
  418. struct rds_ib_mr *ibmr = trans_private;
  419. struct rds_ib_device *rds_ibdev = ibmr->device;
  420. switch (direction) {
  421. case DMA_FROM_DEVICE:
  422. ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
  423. ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
  424. break;
  425. case DMA_TO_DEVICE:
  426. ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
  427. ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
  428. break;
  429. }
  430. }
  431. static void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
  432. {
  433. struct rds_ib_device *rds_ibdev = ibmr->device;
  434. if (ibmr->sg_dma_len) {
  435. ib_dma_unmap_sg(rds_ibdev->dev,
  436. ibmr->sg, ibmr->sg_len,
  437. DMA_BIDIRECTIONAL);
  438. ibmr->sg_dma_len = 0;
  439. }
  440. /* Release the s/g list */
  441. if (ibmr->sg_len) {
  442. unsigned int i;
  443. for (i = 0; i < ibmr->sg_len; ++i) {
  444. struct page *page = sg_page(&ibmr->sg[i]);
  445. /* FIXME we need a way to tell a r/w MR
  446. * from a r/o MR */
  447. WARN_ON(!page->mapping && irqs_disabled());
  448. set_page_dirty(page);
  449. put_page(page);
  450. }
  451. kfree(ibmr->sg);
  452. ibmr->sg = NULL;
  453. ibmr->sg_len = 0;
  454. }
  455. }
  456. static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
  457. {
  458. unsigned int pinned = ibmr->sg_len;
  459. __rds_ib_teardown_mr(ibmr);
  460. if (pinned) {
  461. struct rds_ib_mr_pool *pool = ibmr->pool;
  462. atomic_sub(pinned, &pool->free_pinned);
  463. }
  464. }
  465. static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
  466. {
  467. unsigned int item_count;
  468. item_count = atomic_read(&pool->item_count);
  469. if (free_all)
  470. return item_count;
  471. return 0;
  472. }
  473. /*
  474. * given an llist of mrs, put them all into the list_head for more processing
  475. */
  476. static unsigned int llist_append_to_list(struct llist_head *llist,
  477. struct list_head *list)
  478. {
  479. struct rds_ib_mr *ibmr;
  480. struct llist_node *node;
  481. struct llist_node *next;
  482. unsigned int count = 0;
  483. node = llist_del_all(llist);
  484. while (node) {
  485. next = node->next;
  486. ibmr = llist_entry(node, struct rds_ib_mr, llnode);
  487. list_add_tail(&ibmr->unmap_list, list);
  488. node = next;
  489. count++;
  490. }
  491. return count;
  492. }
  493. /*
  494. * this takes a list head of mrs and turns it into linked llist nodes
  495. * of clusters. Each cluster has linked llist nodes of
  496. * MR_CLUSTER_SIZE mrs that are ready for reuse.
  497. */
  498. static void list_to_llist_nodes(struct rds_ib_mr_pool *pool,
  499. struct list_head *list,
  500. struct llist_node **nodes_head,
  501. struct llist_node **nodes_tail)
  502. {
  503. struct rds_ib_mr *ibmr;
  504. struct llist_node *cur = NULL;
  505. struct llist_node **next = nodes_head;
  506. list_for_each_entry(ibmr, list, unmap_list) {
  507. cur = &ibmr->llnode;
  508. *next = cur;
  509. next = &cur->next;
  510. }
  511. *next = NULL;
  512. *nodes_tail = cur;
  513. }
  514. /*
  515. * Flush our pool of MRs.
  516. * At a minimum, all currently unused MRs are unmapped.
  517. * If the number of MRs allocated exceeds the limit, we also try
  518. * to free as many MRs as needed to get back to this limit.
  519. */
  520. static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
  521. int free_all, struct rds_ib_mr **ibmr_ret)
  522. {
  523. struct rds_ib_mr *ibmr, *next;
  524. struct llist_node *clean_nodes;
  525. struct llist_node *clean_tail;
  526. LIST_HEAD(unmap_list);
  527. LIST_HEAD(fmr_list);
  528. unsigned long unpinned = 0;
  529. unsigned int nfreed = 0, dirty_to_clean = 0, free_goal;
  530. int ret = 0;
  531. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  532. rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush);
  533. else
  534. rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush);
  535. if (ibmr_ret) {
  536. DEFINE_WAIT(wait);
  537. while (!mutex_trylock(&pool->flush_lock)) {
  538. ibmr = rds_ib_reuse_fmr(pool);
  539. if (ibmr) {
  540. *ibmr_ret = ibmr;
  541. finish_wait(&pool->flush_wait, &wait);
  542. goto out_nolock;
  543. }
  544. prepare_to_wait(&pool->flush_wait, &wait,
  545. TASK_UNINTERRUPTIBLE);
  546. if (llist_empty(&pool->clean_list))
  547. schedule();
  548. ibmr = rds_ib_reuse_fmr(pool);
  549. if (ibmr) {
  550. *ibmr_ret = ibmr;
  551. finish_wait(&pool->flush_wait, &wait);
  552. goto out_nolock;
  553. }
  554. }
  555. finish_wait(&pool->flush_wait, &wait);
  556. } else
  557. mutex_lock(&pool->flush_lock);
  558. if (ibmr_ret) {
  559. ibmr = rds_ib_reuse_fmr(pool);
  560. if (ibmr) {
  561. *ibmr_ret = ibmr;
  562. goto out;
  563. }
  564. }
  565. /* Get the list of all MRs to be dropped. Ordering matters -
  566. * we want to put drop_list ahead of free_list.
  567. */
  568. dirty_to_clean = llist_append_to_list(&pool->drop_list, &unmap_list);
  569. dirty_to_clean += llist_append_to_list(&pool->free_list, &unmap_list);
  570. if (free_all)
  571. llist_append_to_list(&pool->clean_list, &unmap_list);
  572. free_goal = rds_ib_flush_goal(pool, free_all);
  573. if (list_empty(&unmap_list))
  574. goto out;
  575. /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
  576. list_for_each_entry(ibmr, &unmap_list, unmap_list)
  577. list_add(&ibmr->fmr->list, &fmr_list);
  578. ret = ib_unmap_fmr(&fmr_list);
  579. if (ret)
  580. printk(KERN_WARNING "RDS/IB: ib_unmap_fmr failed (err=%d)\n", ret);
  581. /* Now we can destroy the DMA mapping and unpin any pages */
  582. list_for_each_entry_safe(ibmr, next, &unmap_list, unmap_list) {
  583. unpinned += ibmr->sg_len;
  584. __rds_ib_teardown_mr(ibmr);
  585. if (nfreed < free_goal ||
  586. ibmr->remap_count >= pool->fmr_attr.max_maps) {
  587. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  588. rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
  589. else
  590. rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
  591. list_del(&ibmr->unmap_list);
  592. ib_dealloc_fmr(ibmr->fmr);
  593. kfree(ibmr);
  594. nfreed++;
  595. }
  596. }
  597. if (!list_empty(&unmap_list)) {
  598. /* we have to make sure that none of the things we're about
  599. * to put on the clean list would race with other cpus trying
  600. * to pull items off. The llist would explode if we managed to
  601. * remove something from the clean list and then add it back again
  602. * while another CPU was spinning on that same item in llist_del_first.
  603. *
  604. * This is pretty unlikely, but just in case wait for an llist grace period
  605. * here before adding anything back into the clean list.
  606. */
  607. wait_clean_list_grace();
  608. list_to_llist_nodes(pool, &unmap_list, &clean_nodes, &clean_tail);
  609. if (ibmr_ret)
  610. *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode);
  611. /* more than one entry in llist nodes */
  612. if (clean_nodes->next)
  613. llist_add_batch(clean_nodes->next, clean_tail, &pool->clean_list);
  614. }
  615. atomic_sub(unpinned, &pool->free_pinned);
  616. atomic_sub(dirty_to_clean, &pool->dirty_count);
  617. atomic_sub(nfreed, &pool->item_count);
  618. out:
  619. mutex_unlock(&pool->flush_lock);
  620. if (waitqueue_active(&pool->flush_wait))
  621. wake_up(&pool->flush_wait);
  622. out_nolock:
  623. return ret;
  624. }
  625. static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
  626. {
  627. struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
  628. rds_ib_flush_mr_pool(pool, 0, NULL);
  629. }
  630. void rds_ib_free_mr(void *trans_private, int invalidate)
  631. {
  632. struct rds_ib_mr *ibmr = trans_private;
  633. struct rds_ib_mr_pool *pool = ibmr->pool;
  634. struct rds_ib_device *rds_ibdev = ibmr->device;
  635. rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
  636. /* Return it to the pool's free list */
  637. if (ibmr->remap_count >= pool->fmr_attr.max_maps)
  638. llist_add(&ibmr->llnode, &pool->drop_list);
  639. else
  640. llist_add(&ibmr->llnode, &pool->free_list);
  641. atomic_add(ibmr->sg_len, &pool->free_pinned);
  642. atomic_inc(&pool->dirty_count);
  643. /* If we've pinned too many pages, request a flush */
  644. if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
  645. atomic_read(&pool->dirty_count) >= pool->max_items / 5)
  646. queue_delayed_work(rds_ib_fmr_wq, &pool->flush_worker, 10);
  647. if (invalidate) {
  648. if (likely(!in_interrupt())) {
  649. rds_ib_flush_mr_pool(pool, 0, NULL);
  650. } else {
  651. /* We get here if the user created a MR marked
  652. * as use_once and invalidate at the same time.
  653. */
  654. queue_delayed_work(rds_ib_fmr_wq,
  655. &pool->flush_worker, 10);
  656. }
  657. }
  658. rds_ib_dev_put(rds_ibdev);
  659. }
  660. void rds_ib_flush_mrs(void)
  661. {
  662. struct rds_ib_device *rds_ibdev;
  663. down_read(&rds_ib_devices_lock);
  664. list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
  665. if (rds_ibdev->mr_8k_pool)
  666. rds_ib_flush_mr_pool(rds_ibdev->mr_8k_pool, 0, NULL);
  667. if (rds_ibdev->mr_1m_pool)
  668. rds_ib_flush_mr_pool(rds_ibdev->mr_1m_pool, 0, NULL);
  669. }
  670. up_read(&rds_ib_devices_lock);
  671. }
  672. void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
  673. struct rds_sock *rs, u32 *key_ret)
  674. {
  675. struct rds_ib_device *rds_ibdev;
  676. struct rds_ib_mr *ibmr = NULL;
  677. int ret;
  678. rds_ibdev = rds_ib_get_device(rs->rs_bound_addr);
  679. if (!rds_ibdev) {
  680. ret = -ENODEV;
  681. goto out;
  682. }
  683. if (!rds_ibdev->mr_8k_pool || !rds_ibdev->mr_1m_pool) {
  684. ret = -ENODEV;
  685. goto out;
  686. }
  687. ibmr = rds_ib_alloc_fmr(rds_ibdev, nents);
  688. if (IS_ERR(ibmr)) {
  689. rds_ib_dev_put(rds_ibdev);
  690. return ibmr;
  691. }
  692. ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents);
  693. if (ret == 0)
  694. *key_ret = ibmr->fmr->rkey;
  695. else
  696. printk(KERN_WARNING "RDS/IB: map_fmr failed (errno=%d)\n", ret);
  697. ibmr->device = rds_ibdev;
  698. rds_ibdev = NULL;
  699. out:
  700. if (ret) {
  701. if (ibmr)
  702. rds_ib_free_mr(ibmr, 0);
  703. ibmr = ERR_PTR(ret);
  704. }
  705. if (rds_ibdev)
  706. rds_ib_dev_put(rds_ibdev);
  707. return ibmr;
  708. }