ib_rdma.c 20 KB

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