fmr_pool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/errno.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/export.h>
  36. #include <linux/slab.h>
  37. #include <linux/jhash.h>
  38. #include <linux/kthread.h>
  39. #include <rdma/ib_fmr_pool.h>
  40. #include "core_priv.h"
  41. #define PFX "fmr_pool: "
  42. enum {
  43. IB_FMR_MAX_REMAPS = 32,
  44. IB_FMR_HASH_BITS = 8,
  45. IB_FMR_HASH_SIZE = 1 << IB_FMR_HASH_BITS,
  46. IB_FMR_HASH_MASK = IB_FMR_HASH_SIZE - 1
  47. };
  48. /*
  49. * If an FMR is not in use, then the list member will point to either
  50. * its pool's free_list (if the FMR can be mapped again; that is,
  51. * remap_count < pool->max_remaps) or its pool's dirty_list (if the
  52. * FMR needs to be unmapped before being remapped). In either of
  53. * these cases it is a bug if the ref_count is not 0. In other words,
  54. * if ref_count is > 0, then the list member must not be linked into
  55. * either free_list or dirty_list.
  56. *
  57. * The cache_node member is used to link the FMR into a cache bucket
  58. * (if caching is enabled). This is independent of the reference
  59. * count of the FMR. When a valid FMR is released, its ref_count is
  60. * decremented, and if ref_count reaches 0, the FMR is placed in
  61. * either free_list or dirty_list as appropriate. However, it is not
  62. * removed from the cache and may be "revived" if a call to
  63. * ib_fmr_register_physical() occurs before the FMR is remapped. In
  64. * this case we just increment the ref_count and remove the FMR from
  65. * free_list/dirty_list.
  66. *
  67. * Before we remap an FMR from free_list, we remove it from the cache
  68. * (to prevent another user from obtaining a stale FMR). When an FMR
  69. * is released, we add it to the tail of the free list, so that our
  70. * cache eviction policy is "least recently used."
  71. *
  72. * All manipulation of ref_count, list and cache_node is protected by
  73. * pool_lock to maintain consistency.
  74. */
  75. struct ib_fmr_pool {
  76. spinlock_t pool_lock;
  77. int pool_size;
  78. int max_pages;
  79. int max_remaps;
  80. int dirty_watermark;
  81. int dirty_len;
  82. struct list_head free_list;
  83. struct list_head dirty_list;
  84. struct hlist_head *cache_bucket;
  85. void (*flush_function)(struct ib_fmr_pool *pool,
  86. void * arg);
  87. void *flush_arg;
  88. struct kthread_worker *worker;
  89. struct kthread_work work;
  90. atomic_t req_ser;
  91. atomic_t flush_ser;
  92. wait_queue_head_t force_wait;
  93. };
  94. static inline u32 ib_fmr_hash(u64 first_page)
  95. {
  96. return jhash_2words((u32) first_page, (u32) (first_page >> 32), 0) &
  97. (IB_FMR_HASH_SIZE - 1);
  98. }
  99. /* Caller must hold pool_lock */
  100. static inline struct ib_pool_fmr *ib_fmr_cache_lookup(struct ib_fmr_pool *pool,
  101. u64 *page_list,
  102. int page_list_len,
  103. u64 io_virtual_address)
  104. {
  105. struct hlist_head *bucket;
  106. struct ib_pool_fmr *fmr;
  107. if (!pool->cache_bucket)
  108. return NULL;
  109. bucket = pool->cache_bucket + ib_fmr_hash(*page_list);
  110. hlist_for_each_entry(fmr, bucket, cache_node)
  111. if (io_virtual_address == fmr->io_virtual_address &&
  112. page_list_len == fmr->page_list_len &&
  113. !memcmp(page_list, fmr->page_list,
  114. page_list_len * sizeof *page_list))
  115. return fmr;
  116. return NULL;
  117. }
  118. static void ib_fmr_batch_release(struct ib_fmr_pool *pool)
  119. {
  120. int ret;
  121. struct ib_pool_fmr *fmr;
  122. LIST_HEAD(unmap_list);
  123. LIST_HEAD(fmr_list);
  124. spin_lock_irq(&pool->pool_lock);
  125. list_for_each_entry(fmr, &pool->dirty_list, list) {
  126. hlist_del_init(&fmr->cache_node);
  127. fmr->remap_count = 0;
  128. list_add_tail(&fmr->fmr->list, &fmr_list);
  129. #ifdef DEBUG
  130. if (fmr->ref_count !=0) {
  131. pr_warn(PFX "Unmapping FMR 0x%08x with ref count %d\n",
  132. fmr, fmr->ref_count);
  133. }
  134. #endif
  135. }
  136. list_splice_init(&pool->dirty_list, &unmap_list);
  137. pool->dirty_len = 0;
  138. spin_unlock_irq(&pool->pool_lock);
  139. if (list_empty(&unmap_list)) {
  140. return;
  141. }
  142. ret = ib_unmap_fmr(&fmr_list);
  143. if (ret)
  144. pr_warn(PFX "ib_unmap_fmr returned %d\n", ret);
  145. spin_lock_irq(&pool->pool_lock);
  146. list_splice(&unmap_list, &pool->free_list);
  147. spin_unlock_irq(&pool->pool_lock);
  148. }
  149. static void ib_fmr_cleanup_func(struct kthread_work *work)
  150. {
  151. struct ib_fmr_pool *pool = container_of(work, struct ib_fmr_pool, work);
  152. ib_fmr_batch_release(pool);
  153. atomic_inc(&pool->flush_ser);
  154. wake_up_interruptible(&pool->force_wait);
  155. if (pool->flush_function)
  156. pool->flush_function(pool, pool->flush_arg);
  157. if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0)
  158. kthread_queue_work(pool->worker, &pool->work);
  159. }
  160. /**
  161. * ib_create_fmr_pool - Create an FMR pool
  162. * @pd:Protection domain for FMRs
  163. * @params:FMR pool parameters
  164. *
  165. * Create a pool of FMRs. Return value is pointer to new pool or
  166. * error code if creation failed.
  167. */
  168. struct ib_fmr_pool *ib_create_fmr_pool(struct ib_pd *pd,
  169. struct ib_fmr_pool_param *params)
  170. {
  171. struct ib_device *device;
  172. struct ib_fmr_pool *pool;
  173. int i;
  174. int ret;
  175. int max_remaps;
  176. if (!params)
  177. return ERR_PTR(-EINVAL);
  178. device = pd->device;
  179. if (!device->alloc_fmr || !device->dealloc_fmr ||
  180. !device->map_phys_fmr || !device->unmap_fmr) {
  181. pr_info(PFX "Device %s does not support FMRs\n", device->name);
  182. return ERR_PTR(-ENOSYS);
  183. }
  184. if (!device->attrs.max_map_per_fmr)
  185. max_remaps = IB_FMR_MAX_REMAPS;
  186. else
  187. max_remaps = device->attrs.max_map_per_fmr;
  188. pool = kmalloc(sizeof *pool, GFP_KERNEL);
  189. if (!pool)
  190. return ERR_PTR(-ENOMEM);
  191. pool->cache_bucket = NULL;
  192. pool->flush_function = params->flush_function;
  193. pool->flush_arg = params->flush_arg;
  194. INIT_LIST_HEAD(&pool->free_list);
  195. INIT_LIST_HEAD(&pool->dirty_list);
  196. if (params->cache) {
  197. pool->cache_bucket =
  198. kmalloc(IB_FMR_HASH_SIZE * sizeof *pool->cache_bucket,
  199. GFP_KERNEL);
  200. if (!pool->cache_bucket) {
  201. ret = -ENOMEM;
  202. goto out_free_pool;
  203. }
  204. for (i = 0; i < IB_FMR_HASH_SIZE; ++i)
  205. INIT_HLIST_HEAD(pool->cache_bucket + i);
  206. }
  207. pool->pool_size = 0;
  208. pool->max_pages = params->max_pages_per_fmr;
  209. pool->max_remaps = max_remaps;
  210. pool->dirty_watermark = params->dirty_watermark;
  211. pool->dirty_len = 0;
  212. spin_lock_init(&pool->pool_lock);
  213. atomic_set(&pool->req_ser, 0);
  214. atomic_set(&pool->flush_ser, 0);
  215. init_waitqueue_head(&pool->force_wait);
  216. pool->worker = kthread_create_worker(0, "ib_fmr(%s)", device->name);
  217. if (IS_ERR(pool->worker)) {
  218. pr_warn(PFX "couldn't start cleanup kthread worker\n");
  219. ret = PTR_ERR(pool->worker);
  220. goto out_free_pool;
  221. }
  222. kthread_init_work(&pool->work, ib_fmr_cleanup_func);
  223. {
  224. struct ib_pool_fmr *fmr;
  225. struct ib_fmr_attr fmr_attr = {
  226. .max_pages = params->max_pages_per_fmr,
  227. .max_maps = pool->max_remaps,
  228. .page_shift = params->page_shift
  229. };
  230. int bytes_per_fmr = sizeof *fmr;
  231. if (pool->cache_bucket)
  232. bytes_per_fmr += params->max_pages_per_fmr * sizeof (u64);
  233. for (i = 0; i < params->pool_size; ++i) {
  234. fmr = kmalloc(bytes_per_fmr, GFP_KERNEL);
  235. if (!fmr)
  236. goto out_fail;
  237. fmr->pool = pool;
  238. fmr->remap_count = 0;
  239. fmr->ref_count = 0;
  240. INIT_HLIST_NODE(&fmr->cache_node);
  241. fmr->fmr = ib_alloc_fmr(pd, params->access, &fmr_attr);
  242. if (IS_ERR(fmr->fmr)) {
  243. pr_warn(PFX "fmr_create failed for FMR %d\n",
  244. i);
  245. kfree(fmr);
  246. goto out_fail;
  247. }
  248. list_add_tail(&fmr->list, &pool->free_list);
  249. ++pool->pool_size;
  250. }
  251. }
  252. return pool;
  253. out_free_pool:
  254. kfree(pool->cache_bucket);
  255. kfree(pool);
  256. return ERR_PTR(ret);
  257. out_fail:
  258. ib_destroy_fmr_pool(pool);
  259. return ERR_PTR(-ENOMEM);
  260. }
  261. EXPORT_SYMBOL(ib_create_fmr_pool);
  262. /**
  263. * ib_destroy_fmr_pool - Free FMR pool
  264. * @pool:FMR pool to free
  265. *
  266. * Destroy an FMR pool and free all associated resources.
  267. */
  268. void ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
  269. {
  270. struct ib_pool_fmr *fmr;
  271. struct ib_pool_fmr *tmp;
  272. LIST_HEAD(fmr_list);
  273. int i;
  274. kthread_destroy_worker(pool->worker);
  275. ib_fmr_batch_release(pool);
  276. i = 0;
  277. list_for_each_entry_safe(fmr, tmp, &pool->free_list, list) {
  278. if (fmr->remap_count) {
  279. INIT_LIST_HEAD(&fmr_list);
  280. list_add_tail(&fmr->fmr->list, &fmr_list);
  281. ib_unmap_fmr(&fmr_list);
  282. }
  283. ib_dealloc_fmr(fmr->fmr);
  284. list_del(&fmr->list);
  285. kfree(fmr);
  286. ++i;
  287. }
  288. if (i < pool->pool_size)
  289. pr_warn(PFX "pool still has %d regions registered\n",
  290. pool->pool_size - i);
  291. kfree(pool->cache_bucket);
  292. kfree(pool);
  293. }
  294. EXPORT_SYMBOL(ib_destroy_fmr_pool);
  295. /**
  296. * ib_flush_fmr_pool - Invalidate all unmapped FMRs
  297. * @pool:FMR pool to flush
  298. *
  299. * Ensure that all unmapped FMRs are fully invalidated.
  300. */
  301. int ib_flush_fmr_pool(struct ib_fmr_pool *pool)
  302. {
  303. int serial;
  304. struct ib_pool_fmr *fmr, *next;
  305. /*
  306. * The free_list holds FMRs that may have been used
  307. * but have not been remapped enough times to be dirty.
  308. * Put them on the dirty list now so that the cleanup
  309. * thread will reap them too.
  310. */
  311. spin_lock_irq(&pool->pool_lock);
  312. list_for_each_entry_safe(fmr, next, &pool->free_list, list) {
  313. if (fmr->remap_count > 0)
  314. list_move(&fmr->list, &pool->dirty_list);
  315. }
  316. spin_unlock_irq(&pool->pool_lock);
  317. serial = atomic_inc_return(&pool->req_ser);
  318. kthread_queue_work(pool->worker, &pool->work);
  319. if (wait_event_interruptible(pool->force_wait,
  320. atomic_read(&pool->flush_ser) - serial >= 0))
  321. return -EINTR;
  322. return 0;
  323. }
  324. EXPORT_SYMBOL(ib_flush_fmr_pool);
  325. /**
  326. * ib_fmr_pool_map_phys -
  327. * @pool:FMR pool to allocate FMR from
  328. * @page_list:List of pages to map
  329. * @list_len:Number of pages in @page_list
  330. * @io_virtual_address:I/O virtual address for new FMR
  331. *
  332. * Map an FMR from an FMR pool.
  333. */
  334. struct ib_pool_fmr *ib_fmr_pool_map_phys(struct ib_fmr_pool *pool_handle,
  335. u64 *page_list,
  336. int list_len,
  337. u64 io_virtual_address)
  338. {
  339. struct ib_fmr_pool *pool = pool_handle;
  340. struct ib_pool_fmr *fmr;
  341. unsigned long flags;
  342. int result;
  343. if (list_len < 1 || list_len > pool->max_pages)
  344. return ERR_PTR(-EINVAL);
  345. spin_lock_irqsave(&pool->pool_lock, flags);
  346. fmr = ib_fmr_cache_lookup(pool,
  347. page_list,
  348. list_len,
  349. io_virtual_address);
  350. if (fmr) {
  351. /* found in cache */
  352. ++fmr->ref_count;
  353. if (fmr->ref_count == 1) {
  354. list_del(&fmr->list);
  355. }
  356. spin_unlock_irqrestore(&pool->pool_lock, flags);
  357. return fmr;
  358. }
  359. if (list_empty(&pool->free_list)) {
  360. spin_unlock_irqrestore(&pool->pool_lock, flags);
  361. return ERR_PTR(-EAGAIN);
  362. }
  363. fmr = list_entry(pool->free_list.next, struct ib_pool_fmr, list);
  364. list_del(&fmr->list);
  365. hlist_del_init(&fmr->cache_node);
  366. spin_unlock_irqrestore(&pool->pool_lock, flags);
  367. result = ib_map_phys_fmr(fmr->fmr, page_list, list_len,
  368. io_virtual_address);
  369. if (result) {
  370. spin_lock_irqsave(&pool->pool_lock, flags);
  371. list_add(&fmr->list, &pool->free_list);
  372. spin_unlock_irqrestore(&pool->pool_lock, flags);
  373. pr_warn(PFX "fmr_map returns %d\n", result);
  374. return ERR_PTR(result);
  375. }
  376. ++fmr->remap_count;
  377. fmr->ref_count = 1;
  378. if (pool->cache_bucket) {
  379. fmr->io_virtual_address = io_virtual_address;
  380. fmr->page_list_len = list_len;
  381. memcpy(fmr->page_list, page_list, list_len * sizeof(*page_list));
  382. spin_lock_irqsave(&pool->pool_lock, flags);
  383. hlist_add_head(&fmr->cache_node,
  384. pool->cache_bucket + ib_fmr_hash(fmr->page_list[0]));
  385. spin_unlock_irqrestore(&pool->pool_lock, flags);
  386. }
  387. return fmr;
  388. }
  389. EXPORT_SYMBOL(ib_fmr_pool_map_phys);
  390. /**
  391. * ib_fmr_pool_unmap - Unmap FMR
  392. * @fmr:FMR to unmap
  393. *
  394. * Unmap an FMR. The FMR mapping may remain valid until the FMR is
  395. * reused (or until ib_flush_fmr_pool() is called).
  396. */
  397. int ib_fmr_pool_unmap(struct ib_pool_fmr *fmr)
  398. {
  399. struct ib_fmr_pool *pool;
  400. unsigned long flags;
  401. pool = fmr->pool;
  402. spin_lock_irqsave(&pool->pool_lock, flags);
  403. --fmr->ref_count;
  404. if (!fmr->ref_count) {
  405. if (fmr->remap_count < pool->max_remaps) {
  406. list_add_tail(&fmr->list, &pool->free_list);
  407. } else {
  408. list_add_tail(&fmr->list, &pool->dirty_list);
  409. if (++pool->dirty_len >= pool->dirty_watermark) {
  410. atomic_inc(&pool->req_ser);
  411. kthread_queue_work(pool->worker, &pool->work);
  412. }
  413. }
  414. }
  415. #ifdef DEBUG
  416. if (fmr->ref_count < 0)
  417. pr_warn(PFX "FMR %p has ref count %d < 0\n",
  418. fmr, fmr->ref_count);
  419. #endif
  420. spin_unlock_irqrestore(&pool->pool_lock, flags);
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(ib_fmr_pool_unmap);