ib_rdma.c 17 KB

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