ib_rdma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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_single_path.h"
  38. #include "ib_mr.h"
  39. struct workqueue_struct *rds_ib_mr_wq;
  40. static DEFINE_PER_CPU(unsigned long, clean_list_grace);
  41. #define CLEAN_LIST_BUSY_BIT 0
  42. static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
  43. {
  44. struct rds_ib_device *rds_ibdev;
  45. struct rds_ib_ipaddr *i_ipaddr;
  46. rcu_read_lock();
  47. list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) {
  48. list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
  49. if (i_ipaddr->ipaddr == ipaddr) {
  50. refcount_inc(&rds_ibdev->refcount);
  51. rcu_read_unlock();
  52. return rds_ibdev;
  53. }
  54. }
  55. }
  56. rcu_read_unlock();
  57. return NULL;
  58. }
  59. static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
  60. {
  61. struct rds_ib_ipaddr *i_ipaddr;
  62. i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
  63. if (!i_ipaddr)
  64. return -ENOMEM;
  65. i_ipaddr->ipaddr = ipaddr;
  66. spin_lock_irq(&rds_ibdev->spinlock);
  67. list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
  68. spin_unlock_irq(&rds_ibdev->spinlock);
  69. return 0;
  70. }
  71. static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
  72. {
  73. struct rds_ib_ipaddr *i_ipaddr;
  74. struct rds_ib_ipaddr *to_free = NULL;
  75. spin_lock_irq(&rds_ibdev->spinlock);
  76. list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
  77. if (i_ipaddr->ipaddr == ipaddr) {
  78. list_del_rcu(&i_ipaddr->list);
  79. to_free = i_ipaddr;
  80. break;
  81. }
  82. }
  83. spin_unlock_irq(&rds_ibdev->spinlock);
  84. if (to_free)
  85. kfree_rcu(to_free, rcu);
  86. }
  87. int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
  88. {
  89. struct rds_ib_device *rds_ibdev_old;
  90. rds_ibdev_old = rds_ib_get_device(ipaddr);
  91. if (!rds_ibdev_old)
  92. return rds_ib_add_ipaddr(rds_ibdev, ipaddr);
  93. if (rds_ibdev_old != rds_ibdev) {
  94. rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr);
  95. rds_ib_dev_put(rds_ibdev_old);
  96. return rds_ib_add_ipaddr(rds_ibdev, ipaddr);
  97. }
  98. rds_ib_dev_put(rds_ibdev_old);
  99. return 0;
  100. }
  101. void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
  102. {
  103. struct rds_ib_connection *ic = conn->c_transport_data;
  104. /* conn was previously on the nodev_conns_list */
  105. spin_lock_irq(&ib_nodev_conns_lock);
  106. BUG_ON(list_empty(&ib_nodev_conns));
  107. BUG_ON(list_empty(&ic->ib_node));
  108. list_del(&ic->ib_node);
  109. spin_lock(&rds_ibdev->spinlock);
  110. list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
  111. spin_unlock(&rds_ibdev->spinlock);
  112. spin_unlock_irq(&ib_nodev_conns_lock);
  113. ic->rds_ibdev = rds_ibdev;
  114. refcount_inc(&rds_ibdev->refcount);
  115. }
  116. void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
  117. {
  118. struct rds_ib_connection *ic = conn->c_transport_data;
  119. /* place conn on nodev_conns_list */
  120. spin_lock(&ib_nodev_conns_lock);
  121. spin_lock_irq(&rds_ibdev->spinlock);
  122. BUG_ON(list_empty(&ic->ib_node));
  123. list_del(&ic->ib_node);
  124. spin_unlock_irq(&rds_ibdev->spinlock);
  125. list_add_tail(&ic->ib_node, &ib_nodev_conns);
  126. spin_unlock(&ib_nodev_conns_lock);
  127. ic->rds_ibdev = NULL;
  128. rds_ib_dev_put(rds_ibdev);
  129. }
  130. void rds_ib_destroy_nodev_conns(void)
  131. {
  132. struct rds_ib_connection *ic, *_ic;
  133. LIST_HEAD(tmp_list);
  134. /* avoid calling conn_destroy with irqs off */
  135. spin_lock_irq(&ib_nodev_conns_lock);
  136. list_splice(&ib_nodev_conns, &tmp_list);
  137. spin_unlock_irq(&ib_nodev_conns_lock);
  138. list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
  139. rds_conn_destroy(ic->conn);
  140. }
  141. void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
  142. {
  143. struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
  144. iinfo->rdma_mr_max = pool_1m->max_items;
  145. iinfo->rdma_mr_size = pool_1m->fmr_attr.max_pages;
  146. }
  147. struct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *pool)
  148. {
  149. struct rds_ib_mr *ibmr = NULL;
  150. struct llist_node *ret;
  151. unsigned long *flag;
  152. preempt_disable();
  153. flag = this_cpu_ptr(&clean_list_grace);
  154. set_bit(CLEAN_LIST_BUSY_BIT, flag);
  155. ret = llist_del_first(&pool->clean_list);
  156. if (ret) {
  157. ibmr = llist_entry(ret, struct rds_ib_mr, llnode);
  158. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  159. rds_ib_stats_inc(s_ib_rdma_mr_8k_reused);
  160. else
  161. rds_ib_stats_inc(s_ib_rdma_mr_1m_reused);
  162. }
  163. clear_bit(CLEAN_LIST_BUSY_BIT, flag);
  164. preempt_enable();
  165. return ibmr;
  166. }
  167. static inline void wait_clean_list_grace(void)
  168. {
  169. int cpu;
  170. unsigned long *flag;
  171. for_each_online_cpu(cpu) {
  172. flag = &per_cpu(clean_list_grace, cpu);
  173. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  174. cpu_relax();
  175. }
  176. }
  177. void rds_ib_sync_mr(void *trans_private, int direction)
  178. {
  179. struct rds_ib_mr *ibmr = trans_private;
  180. struct rds_ib_device *rds_ibdev = ibmr->device;
  181. switch (direction) {
  182. case DMA_FROM_DEVICE:
  183. ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
  184. ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
  185. break;
  186. case DMA_TO_DEVICE:
  187. ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
  188. ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
  189. break;
  190. }
  191. }
  192. void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
  193. {
  194. struct rds_ib_device *rds_ibdev = ibmr->device;
  195. if (ibmr->sg_dma_len) {
  196. ib_dma_unmap_sg(rds_ibdev->dev,
  197. ibmr->sg, ibmr->sg_len,
  198. DMA_BIDIRECTIONAL);
  199. ibmr->sg_dma_len = 0;
  200. }
  201. /* Release the s/g list */
  202. if (ibmr->sg_len) {
  203. unsigned int i;
  204. for (i = 0; i < ibmr->sg_len; ++i) {
  205. struct page *page = sg_page(&ibmr->sg[i]);
  206. /* FIXME we need a way to tell a r/w MR
  207. * from a r/o MR */
  208. WARN_ON(!page->mapping && irqs_disabled());
  209. set_page_dirty(page);
  210. put_page(page);
  211. }
  212. kfree(ibmr->sg);
  213. ibmr->sg = NULL;
  214. ibmr->sg_len = 0;
  215. }
  216. }
  217. void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
  218. {
  219. unsigned int pinned = ibmr->sg_len;
  220. __rds_ib_teardown_mr(ibmr);
  221. if (pinned) {
  222. struct rds_ib_mr_pool *pool = ibmr->pool;
  223. atomic_sub(pinned, &pool->free_pinned);
  224. }
  225. }
  226. static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
  227. {
  228. unsigned int item_count;
  229. item_count = atomic_read(&pool->item_count);
  230. if (free_all)
  231. return item_count;
  232. return 0;
  233. }
  234. /*
  235. * given an llist of mrs, put them all into the list_head for more processing
  236. */
  237. static unsigned int llist_append_to_list(struct llist_head *llist,
  238. struct list_head *list)
  239. {
  240. struct rds_ib_mr *ibmr;
  241. struct llist_node *node;
  242. struct llist_node *next;
  243. unsigned int count = 0;
  244. node = llist_del_all(llist);
  245. while (node) {
  246. next = node->next;
  247. ibmr = llist_entry(node, struct rds_ib_mr, llnode);
  248. list_add_tail(&ibmr->unmap_list, list);
  249. node = next;
  250. count++;
  251. }
  252. return count;
  253. }
  254. /*
  255. * this takes a list head of mrs and turns it into linked llist nodes
  256. * of clusters. Each cluster has linked llist nodes of
  257. * MR_CLUSTER_SIZE mrs that are ready for reuse.
  258. */
  259. static void list_to_llist_nodes(struct rds_ib_mr_pool *pool,
  260. struct list_head *list,
  261. struct llist_node **nodes_head,
  262. struct llist_node **nodes_tail)
  263. {
  264. struct rds_ib_mr *ibmr;
  265. struct llist_node *cur = NULL;
  266. struct llist_node **next = nodes_head;
  267. list_for_each_entry(ibmr, list, unmap_list) {
  268. cur = &ibmr->llnode;
  269. *next = cur;
  270. next = &cur->next;
  271. }
  272. *next = NULL;
  273. *nodes_tail = cur;
  274. }
  275. /*
  276. * Flush our pool of MRs.
  277. * At a minimum, all currently unused MRs are unmapped.
  278. * If the number of MRs allocated exceeds the limit, we also try
  279. * to free as many MRs as needed to get back to this limit.
  280. */
  281. int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
  282. int free_all, struct rds_ib_mr **ibmr_ret)
  283. {
  284. struct rds_ib_mr *ibmr;
  285. struct llist_node *clean_nodes;
  286. struct llist_node *clean_tail;
  287. LIST_HEAD(unmap_list);
  288. unsigned long unpinned = 0;
  289. unsigned int nfreed = 0, dirty_to_clean = 0, free_goal;
  290. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  291. rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush);
  292. else
  293. rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush);
  294. if (ibmr_ret) {
  295. DEFINE_WAIT(wait);
  296. while (!mutex_trylock(&pool->flush_lock)) {
  297. ibmr = rds_ib_reuse_mr(pool);
  298. if (ibmr) {
  299. *ibmr_ret = ibmr;
  300. finish_wait(&pool->flush_wait, &wait);
  301. goto out_nolock;
  302. }
  303. prepare_to_wait(&pool->flush_wait, &wait,
  304. TASK_UNINTERRUPTIBLE);
  305. if (llist_empty(&pool->clean_list))
  306. schedule();
  307. ibmr = rds_ib_reuse_mr(pool);
  308. if (ibmr) {
  309. *ibmr_ret = ibmr;
  310. finish_wait(&pool->flush_wait, &wait);
  311. goto out_nolock;
  312. }
  313. }
  314. finish_wait(&pool->flush_wait, &wait);
  315. } else
  316. mutex_lock(&pool->flush_lock);
  317. if (ibmr_ret) {
  318. ibmr = rds_ib_reuse_mr(pool);
  319. if (ibmr) {
  320. *ibmr_ret = ibmr;
  321. goto out;
  322. }
  323. }
  324. /* Get the list of all MRs to be dropped. Ordering matters -
  325. * we want to put drop_list ahead of free_list.
  326. */
  327. dirty_to_clean = llist_append_to_list(&pool->drop_list, &unmap_list);
  328. dirty_to_clean += llist_append_to_list(&pool->free_list, &unmap_list);
  329. if (free_all)
  330. llist_append_to_list(&pool->clean_list, &unmap_list);
  331. free_goal = rds_ib_flush_goal(pool, free_all);
  332. if (list_empty(&unmap_list))
  333. goto out;
  334. if (pool->use_fastreg)
  335. rds_ib_unreg_frmr(&unmap_list, &nfreed, &unpinned, free_goal);
  336. else
  337. rds_ib_unreg_fmr(&unmap_list, &nfreed, &unpinned, free_goal);
  338. if (!list_empty(&unmap_list)) {
  339. /* we have to make sure that none of the things we're about
  340. * to put on the clean list would race with other cpus trying
  341. * to pull items off. The llist would explode if we managed to
  342. * remove something from the clean list and then add it back again
  343. * while another CPU was spinning on that same item in llist_del_first.
  344. *
  345. * This is pretty unlikely, but just in case wait for an llist grace period
  346. * here before adding anything back into the clean list.
  347. */
  348. wait_clean_list_grace();
  349. list_to_llist_nodes(pool, &unmap_list, &clean_nodes, &clean_tail);
  350. if (ibmr_ret) {
  351. *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode);
  352. clean_nodes = clean_nodes->next;
  353. }
  354. /* more than one entry in llist nodes */
  355. if (clean_nodes)
  356. llist_add_batch(clean_nodes, clean_tail,
  357. &pool->clean_list);
  358. }
  359. atomic_sub(unpinned, &pool->free_pinned);
  360. atomic_sub(dirty_to_clean, &pool->dirty_count);
  361. atomic_sub(nfreed, &pool->item_count);
  362. out:
  363. mutex_unlock(&pool->flush_lock);
  364. if (waitqueue_active(&pool->flush_wait))
  365. wake_up(&pool->flush_wait);
  366. out_nolock:
  367. return 0;
  368. }
  369. struct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *pool)
  370. {
  371. struct rds_ib_mr *ibmr = NULL;
  372. int iter = 0;
  373. while (1) {
  374. ibmr = rds_ib_reuse_mr(pool);
  375. if (ibmr)
  376. return ibmr;
  377. if (atomic_inc_return(&pool->item_count) <= pool->max_items)
  378. break;
  379. atomic_dec(&pool->item_count);
  380. if (++iter > 2) {
  381. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  382. rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted);
  383. else
  384. rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted);
  385. return ERR_PTR(-EAGAIN);
  386. }
  387. /* We do have some empty MRs. Flush them out. */
  388. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  389. rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait);
  390. else
  391. rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait);
  392. rds_ib_flush_mr_pool(pool, 0, &ibmr);
  393. if (ibmr)
  394. return ibmr;
  395. }
  396. return ibmr;
  397. }
  398. static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
  399. {
  400. struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
  401. rds_ib_flush_mr_pool(pool, 0, NULL);
  402. }
  403. void rds_ib_free_mr(void *trans_private, int invalidate)
  404. {
  405. struct rds_ib_mr *ibmr = trans_private;
  406. struct rds_ib_mr_pool *pool = ibmr->pool;
  407. struct rds_ib_device *rds_ibdev = ibmr->device;
  408. rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
  409. /* Return it to the pool's free list */
  410. if (rds_ibdev->use_fastreg)
  411. rds_ib_free_frmr_list(ibmr);
  412. else
  413. rds_ib_free_fmr_list(ibmr);
  414. atomic_add(ibmr->sg_len, &pool->free_pinned);
  415. atomic_inc(&pool->dirty_count);
  416. /* If we've pinned too many pages, request a flush */
  417. if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
  418. atomic_read(&pool->dirty_count) >= pool->max_items / 5)
  419. queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
  420. if (invalidate) {
  421. if (likely(!in_interrupt())) {
  422. rds_ib_flush_mr_pool(pool, 0, NULL);
  423. } else {
  424. /* We get here if the user created a MR marked
  425. * as use_once and invalidate at the same time.
  426. */
  427. queue_delayed_work(rds_ib_mr_wq,
  428. &pool->flush_worker, 10);
  429. }
  430. }
  431. rds_ib_dev_put(rds_ibdev);
  432. }
  433. void rds_ib_flush_mrs(void)
  434. {
  435. struct rds_ib_device *rds_ibdev;
  436. down_read(&rds_ib_devices_lock);
  437. list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
  438. if (rds_ibdev->mr_8k_pool)
  439. rds_ib_flush_mr_pool(rds_ibdev->mr_8k_pool, 0, NULL);
  440. if (rds_ibdev->mr_1m_pool)
  441. rds_ib_flush_mr_pool(rds_ibdev->mr_1m_pool, 0, NULL);
  442. }
  443. up_read(&rds_ib_devices_lock);
  444. }
  445. void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
  446. struct rds_sock *rs, u32 *key_ret)
  447. {
  448. struct rds_ib_device *rds_ibdev;
  449. struct rds_ib_mr *ibmr = NULL;
  450. struct rds_ib_connection *ic = rs->rs_conn->c_transport_data;
  451. int ret;
  452. rds_ibdev = rds_ib_get_device(rs->rs_bound_addr);
  453. if (!rds_ibdev) {
  454. ret = -ENODEV;
  455. goto out;
  456. }
  457. if (!rds_ibdev->mr_8k_pool || !rds_ibdev->mr_1m_pool) {
  458. ret = -ENODEV;
  459. goto out;
  460. }
  461. if (rds_ibdev->use_fastreg)
  462. ibmr = rds_ib_reg_frmr(rds_ibdev, ic, sg, nents, key_ret);
  463. else
  464. ibmr = rds_ib_reg_fmr(rds_ibdev, sg, nents, key_ret);
  465. if (ibmr)
  466. rds_ibdev = NULL;
  467. out:
  468. if (!ibmr)
  469. pr_warn("RDS/IB: rds_ib_get_mr failed (errno=%d)\n", ret);
  470. if (rds_ibdev)
  471. rds_ib_dev_put(rds_ibdev);
  472. return ibmr;
  473. }
  474. void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
  475. {
  476. cancel_delayed_work_sync(&pool->flush_worker);
  477. rds_ib_flush_mr_pool(pool, 1, NULL);
  478. WARN_ON(atomic_read(&pool->item_count));
  479. WARN_ON(atomic_read(&pool->free_pinned));
  480. kfree(pool);
  481. }
  482. struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev,
  483. int pool_type)
  484. {
  485. struct rds_ib_mr_pool *pool;
  486. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  487. if (!pool)
  488. return ERR_PTR(-ENOMEM);
  489. pool->pool_type = pool_type;
  490. init_llist_head(&pool->free_list);
  491. init_llist_head(&pool->drop_list);
  492. init_llist_head(&pool->clean_list);
  493. mutex_init(&pool->flush_lock);
  494. init_waitqueue_head(&pool->flush_wait);
  495. INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
  496. if (pool_type == RDS_IB_MR_1M_POOL) {
  497. /* +1 allows for unaligned MRs */
  498. pool->fmr_attr.max_pages = RDS_MR_1M_MSG_SIZE + 1;
  499. pool->max_items = RDS_MR_1M_POOL_SIZE;
  500. } else {
  501. /* pool_type == RDS_IB_MR_8K_POOL */
  502. pool->fmr_attr.max_pages = RDS_MR_8K_MSG_SIZE + 1;
  503. pool->max_items = RDS_MR_8K_POOL_SIZE;
  504. }
  505. pool->max_free_pinned = pool->max_items * pool->fmr_attr.max_pages / 4;
  506. pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps;
  507. pool->fmr_attr.page_shift = PAGE_SHIFT;
  508. pool->max_items_soft = rds_ibdev->max_mrs * 3 / 4;
  509. pool->use_fastreg = rds_ibdev->use_fastreg;
  510. return pool;
  511. }
  512. int rds_ib_mr_init(void)
  513. {
  514. rds_ib_mr_wq = alloc_workqueue("rds_mr_flushd", WQ_MEM_RECLAIM, 0);
  515. if (!rds_ib_mr_wq)
  516. return -ENOMEM;
  517. return 0;
  518. }
  519. /* By the time this is called all the IB devices should have been torn down and
  520. * had their pools freed. As each pool is freed its work struct is waited on,
  521. * so the pool flushing work queue should be idle by the time we get here.
  522. */
  523. void rds_ib_mr_exit(void)
  524. {
  525. destroy_workqueue(rds_ib_mr_wq);
  526. }