fmr_pool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. * $Id: fmr_pool.c 2730 2005-06-28 16:43:03Z sean.hefty $
  34. */
  35. #include <linux/errno.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/slab.h>
  38. #include <linux/jhash.h>
  39. #include <linux/kthread.h>
  40. #include <rdma/ib_fmr_pool.h>
  41. #include "core_priv.h"
  42. #define PFX "fmr_pool: "
  43. enum {
  44. IB_FMR_MAX_REMAPS = 32,
  45. IB_FMR_HASH_BITS = 8,
  46. IB_FMR_HASH_SIZE = 1 << IB_FMR_HASH_BITS,
  47. IB_FMR_HASH_MASK = IB_FMR_HASH_SIZE - 1
  48. };
  49. /*
  50. * If an FMR is not in use, then the list member will point to either
  51. * its pool's free_list (if the FMR can be mapped again; that is,
  52. * remap_count < pool->max_remaps) or its pool's dirty_list (if the
  53. * FMR needs to be unmapped before being remapped). In either of
  54. * these cases it is a bug if the ref_count is not 0. In other words,
  55. * if ref_count is > 0, then the list member must not be linked into
  56. * either free_list or dirty_list.
  57. *
  58. * The cache_node member is used to link the FMR into a cache bucket
  59. * (if caching is enabled). This is independent of the reference
  60. * count of the FMR. When a valid FMR is released, its ref_count is
  61. * decremented, and if ref_count reaches 0, the FMR is placed in
  62. * either free_list or dirty_list as appropriate. However, it is not
  63. * removed from the cache and may be "revived" if a call to
  64. * ib_fmr_register_physical() occurs before the FMR is remapped. In
  65. * this case we just increment the ref_count and remove the FMR from
  66. * free_list/dirty_list.
  67. *
  68. * Before we remap an FMR from free_list, we remove it from the cache
  69. * (to prevent another user from obtaining a stale FMR). When an FMR
  70. * is released, we add it to the tail of the free list, so that our
  71. * cache eviction policy is "least recently used."
  72. *
  73. * All manipulation of ref_count, list and cache_node is protected by
  74. * pool_lock to maintain consistency.
  75. */
  76. struct ib_fmr_pool {
  77. spinlock_t pool_lock;
  78. int pool_size;
  79. int max_pages;
  80. int max_remaps;
  81. int dirty_watermark;
  82. int dirty_len;
  83. struct list_head free_list;
  84. struct list_head dirty_list;
  85. struct hlist_head *cache_bucket;
  86. void (*flush_function)(struct ib_fmr_pool *pool,
  87. void * arg);
  88. void *flush_arg;
  89. struct task_struct *thread;
  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. struct hlist_node *pos;
  108. if (!pool->cache_bucket)
  109. return NULL;
  110. bucket = pool->cache_bucket + ib_fmr_hash(*page_list);
  111. hlist_for_each_entry(fmr, pos, bucket, cache_node)
  112. if (io_virtual_address == fmr->io_virtual_address &&
  113. page_list_len == fmr->page_list_len &&
  114. !memcmp(page_list, fmr->page_list,
  115. page_list_len * sizeof *page_list))
  116. return fmr;
  117. return NULL;
  118. }
  119. static void ib_fmr_batch_release(struct ib_fmr_pool *pool)
  120. {
  121. int ret;
  122. struct ib_pool_fmr *fmr;
  123. LIST_HEAD(unmap_list);
  124. LIST_HEAD(fmr_list);
  125. spin_lock_irq(&pool->pool_lock);
  126. list_for_each_entry(fmr, &pool->dirty_list, list) {
  127. hlist_del_init(&fmr->cache_node);
  128. fmr->remap_count = 0;
  129. list_add_tail(&fmr->fmr->list, &fmr_list);
  130. #ifdef DEBUG
  131. if (fmr->ref_count !=0) {
  132. printk(KERN_WARNING PFX "Unmapping FMR 0x%08x with ref count %d",
  133. fmr, fmr->ref_count);
  134. }
  135. #endif
  136. }
  137. list_splice(&pool->dirty_list, &unmap_list);
  138. INIT_LIST_HEAD(&pool->dirty_list);
  139. pool->dirty_len = 0;
  140. spin_unlock_irq(&pool->pool_lock);
  141. if (list_empty(&unmap_list)) {
  142. return;
  143. }
  144. ret = ib_unmap_fmr(&fmr_list);
  145. if (ret)
  146. printk(KERN_WARNING PFX "ib_unmap_fmr returned %d", ret);
  147. spin_lock_irq(&pool->pool_lock);
  148. list_splice(&unmap_list, &pool->free_list);
  149. spin_unlock_irq(&pool->pool_lock);
  150. }
  151. static int ib_fmr_cleanup_thread(void *pool_ptr)
  152. {
  153. struct ib_fmr_pool *pool = pool_ptr;
  154. do {
  155. if (pool->dirty_len >= pool->dirty_watermark ||
  156. atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0) {
  157. ib_fmr_batch_release(pool);
  158. atomic_inc(&pool->flush_ser);
  159. wake_up_interruptible(&pool->force_wait);
  160. if (pool->flush_function)
  161. pool->flush_function(pool, pool->flush_arg);
  162. }
  163. set_current_state(TASK_INTERRUPTIBLE);
  164. if (pool->dirty_len < pool->dirty_watermark &&
  165. atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0 &&
  166. !kthread_should_stop())
  167. schedule();
  168. __set_current_state(TASK_RUNNING);
  169. } while (!kthread_should_stop());
  170. return 0;
  171. }
  172. /**
  173. * ib_create_fmr_pool - Create an FMR pool
  174. * @pd:Protection domain for FMRs
  175. * @params:FMR pool parameters
  176. *
  177. * Create a pool of FMRs. Return value is pointer to new pool or
  178. * error code if creation failed.
  179. */
  180. struct ib_fmr_pool *ib_create_fmr_pool(struct ib_pd *pd,
  181. struct ib_fmr_pool_param *params)
  182. {
  183. struct ib_device *device;
  184. struct ib_fmr_pool *pool;
  185. struct ib_device_attr *attr;
  186. int i;
  187. int ret;
  188. int max_remaps;
  189. if (!params)
  190. return ERR_PTR(-EINVAL);
  191. device = pd->device;
  192. if (!device->alloc_fmr || !device->dealloc_fmr ||
  193. !device->map_phys_fmr || !device->unmap_fmr) {
  194. printk(KERN_INFO PFX "Device %s does not support FMRs\n",
  195. device->name);
  196. return ERR_PTR(-ENOSYS);
  197. }
  198. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  199. if (!attr) {
  200. printk(KERN_WARNING PFX "couldn't allocate device attr struct");
  201. return ERR_PTR(-ENOMEM);
  202. }
  203. ret = ib_query_device(device, attr);
  204. if (ret) {
  205. printk(KERN_WARNING PFX "couldn't query device: %d", ret);
  206. kfree(attr);
  207. return ERR_PTR(ret);
  208. }
  209. if (!attr->max_map_per_fmr)
  210. max_remaps = IB_FMR_MAX_REMAPS;
  211. else
  212. max_remaps = attr->max_map_per_fmr;
  213. kfree(attr);
  214. pool = kmalloc(sizeof *pool, GFP_KERNEL);
  215. if (!pool) {
  216. printk(KERN_WARNING PFX "couldn't allocate pool struct");
  217. return ERR_PTR(-ENOMEM);
  218. }
  219. pool->cache_bucket = NULL;
  220. pool->flush_function = params->flush_function;
  221. pool->flush_arg = params->flush_arg;
  222. INIT_LIST_HEAD(&pool->free_list);
  223. INIT_LIST_HEAD(&pool->dirty_list);
  224. if (params->cache) {
  225. pool->cache_bucket =
  226. kmalloc(IB_FMR_HASH_SIZE * sizeof *pool->cache_bucket,
  227. GFP_KERNEL);
  228. if (!pool->cache_bucket) {
  229. printk(KERN_WARNING PFX "Failed to allocate cache in pool");
  230. ret = -ENOMEM;
  231. goto out_free_pool;
  232. }
  233. for (i = 0; i < IB_FMR_HASH_SIZE; ++i)
  234. INIT_HLIST_HEAD(pool->cache_bucket + i);
  235. }
  236. pool->pool_size = 0;
  237. pool->max_pages = params->max_pages_per_fmr;
  238. pool->max_remaps = max_remaps;
  239. pool->dirty_watermark = params->dirty_watermark;
  240. pool->dirty_len = 0;
  241. spin_lock_init(&pool->pool_lock);
  242. atomic_set(&pool->req_ser, 0);
  243. atomic_set(&pool->flush_ser, 0);
  244. init_waitqueue_head(&pool->force_wait);
  245. pool->thread = kthread_create(ib_fmr_cleanup_thread,
  246. pool,
  247. "ib_fmr(%s)",
  248. device->name);
  249. if (IS_ERR(pool->thread)) {
  250. printk(KERN_WARNING PFX "couldn't start cleanup thread");
  251. ret = PTR_ERR(pool->thread);
  252. goto out_free_pool;
  253. }
  254. {
  255. struct ib_pool_fmr *fmr;
  256. struct ib_fmr_attr fmr_attr = {
  257. .max_pages = params->max_pages_per_fmr,
  258. .max_maps = pool->max_remaps,
  259. .page_shift = params->page_shift
  260. };
  261. for (i = 0; i < params->pool_size; ++i) {
  262. fmr = kmalloc(sizeof *fmr + params->max_pages_per_fmr * sizeof (u64),
  263. GFP_KERNEL);
  264. if (!fmr) {
  265. printk(KERN_WARNING PFX "failed to allocate fmr "
  266. "struct for FMR %d", i);
  267. goto out_fail;
  268. }
  269. fmr->pool = pool;
  270. fmr->remap_count = 0;
  271. fmr->ref_count = 0;
  272. INIT_HLIST_NODE(&fmr->cache_node);
  273. fmr->fmr = ib_alloc_fmr(pd, params->access, &fmr_attr);
  274. if (IS_ERR(fmr->fmr)) {
  275. printk(KERN_WARNING PFX "fmr_create failed "
  276. "for FMR %d", i);
  277. kfree(fmr);
  278. goto out_fail;
  279. }
  280. list_add_tail(&fmr->list, &pool->free_list);
  281. ++pool->pool_size;
  282. }
  283. }
  284. return pool;
  285. out_free_pool:
  286. kfree(pool->cache_bucket);
  287. kfree(pool);
  288. return ERR_PTR(ret);
  289. out_fail:
  290. ib_destroy_fmr_pool(pool);
  291. return ERR_PTR(-ENOMEM);
  292. }
  293. EXPORT_SYMBOL(ib_create_fmr_pool);
  294. /**
  295. * ib_destroy_fmr_pool - Free FMR pool
  296. * @pool:FMR pool to free
  297. *
  298. * Destroy an FMR pool and free all associated resources.
  299. */
  300. void ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
  301. {
  302. struct ib_pool_fmr *fmr;
  303. struct ib_pool_fmr *tmp;
  304. LIST_HEAD(fmr_list);
  305. int i;
  306. kthread_stop(pool->thread);
  307. ib_fmr_batch_release(pool);
  308. i = 0;
  309. list_for_each_entry_safe(fmr, tmp, &pool->free_list, list) {
  310. if (fmr->remap_count) {
  311. INIT_LIST_HEAD(&fmr_list);
  312. list_add_tail(&fmr->fmr->list, &fmr_list);
  313. ib_unmap_fmr(&fmr_list);
  314. }
  315. ib_dealloc_fmr(fmr->fmr);
  316. list_del(&fmr->list);
  317. kfree(fmr);
  318. ++i;
  319. }
  320. if (i < pool->pool_size)
  321. printk(KERN_WARNING PFX "pool still has %d regions registered",
  322. pool->pool_size - i);
  323. kfree(pool->cache_bucket);
  324. kfree(pool);
  325. }
  326. EXPORT_SYMBOL(ib_destroy_fmr_pool);
  327. /**
  328. * ib_flush_fmr_pool - Invalidate all unmapped FMRs
  329. * @pool:FMR pool to flush
  330. *
  331. * Ensure that all unmapped FMRs are fully invalidated.
  332. */
  333. int ib_flush_fmr_pool(struct ib_fmr_pool *pool)
  334. {
  335. int serial = atomic_inc_return(&pool->req_ser);
  336. wake_up_process(pool->thread);
  337. if (wait_event_interruptible(pool->force_wait,
  338. atomic_read(&pool->flush_ser) - serial >= 0))
  339. return -EINTR;
  340. return 0;
  341. }
  342. EXPORT_SYMBOL(ib_flush_fmr_pool);
  343. /**
  344. * ib_fmr_pool_map_phys -
  345. * @pool:FMR pool to allocate FMR from
  346. * @page_list:List of pages to map
  347. * @list_len:Number of pages in @page_list
  348. * @io_virtual_address:I/O virtual address for new FMR
  349. *
  350. * Map an FMR from an FMR pool.
  351. */
  352. struct ib_pool_fmr *ib_fmr_pool_map_phys(struct ib_fmr_pool *pool_handle,
  353. u64 *page_list,
  354. int list_len,
  355. u64 io_virtual_address)
  356. {
  357. struct ib_fmr_pool *pool = pool_handle;
  358. struct ib_pool_fmr *fmr;
  359. unsigned long flags;
  360. int result;
  361. if (list_len < 1 || list_len > pool->max_pages)
  362. return ERR_PTR(-EINVAL);
  363. spin_lock_irqsave(&pool->pool_lock, flags);
  364. fmr = ib_fmr_cache_lookup(pool,
  365. page_list,
  366. list_len,
  367. io_virtual_address);
  368. if (fmr) {
  369. /* found in cache */
  370. ++fmr->ref_count;
  371. if (fmr->ref_count == 1) {
  372. list_del(&fmr->list);
  373. }
  374. spin_unlock_irqrestore(&pool->pool_lock, flags);
  375. return fmr;
  376. }
  377. if (list_empty(&pool->free_list)) {
  378. spin_unlock_irqrestore(&pool->pool_lock, flags);
  379. return ERR_PTR(-EAGAIN);
  380. }
  381. fmr = list_entry(pool->free_list.next, struct ib_pool_fmr, list);
  382. list_del(&fmr->list);
  383. hlist_del_init(&fmr->cache_node);
  384. spin_unlock_irqrestore(&pool->pool_lock, flags);
  385. result = ib_map_phys_fmr(fmr->fmr, page_list, list_len,
  386. io_virtual_address);
  387. if (result) {
  388. spin_lock_irqsave(&pool->pool_lock, flags);
  389. list_add(&fmr->list, &pool->free_list);
  390. spin_unlock_irqrestore(&pool->pool_lock, flags);
  391. printk(KERN_WARNING PFX "fmr_map returns %d\n", result);
  392. return ERR_PTR(result);
  393. }
  394. ++fmr->remap_count;
  395. fmr->ref_count = 1;
  396. if (pool->cache_bucket) {
  397. fmr->io_virtual_address = io_virtual_address;
  398. fmr->page_list_len = list_len;
  399. memcpy(fmr->page_list, page_list, list_len * sizeof(*page_list));
  400. spin_lock_irqsave(&pool->pool_lock, flags);
  401. hlist_add_head(&fmr->cache_node,
  402. pool->cache_bucket + ib_fmr_hash(fmr->page_list[0]));
  403. spin_unlock_irqrestore(&pool->pool_lock, flags);
  404. }
  405. return fmr;
  406. }
  407. EXPORT_SYMBOL(ib_fmr_pool_map_phys);
  408. /**
  409. * ib_fmr_pool_unmap - Unmap FMR
  410. * @fmr:FMR to unmap
  411. *
  412. * Unmap an FMR. The FMR mapping may remain valid until the FMR is
  413. * reused (or until ib_flush_fmr_pool() is called).
  414. */
  415. int ib_fmr_pool_unmap(struct ib_pool_fmr *fmr)
  416. {
  417. struct ib_fmr_pool *pool;
  418. unsigned long flags;
  419. pool = fmr->pool;
  420. spin_lock_irqsave(&pool->pool_lock, flags);
  421. --fmr->ref_count;
  422. if (!fmr->ref_count) {
  423. if (fmr->remap_count < pool->max_remaps) {
  424. list_add_tail(&fmr->list, &pool->free_list);
  425. } else {
  426. list_add_tail(&fmr->list, &pool->dirty_list);
  427. ++pool->dirty_len;
  428. wake_up_process(pool->thread);
  429. }
  430. }
  431. #ifdef DEBUG
  432. if (fmr->ref_count < 0)
  433. printk(KERN_WARNING PFX "FMR %p has ref count %d < 0",
  434. fmr, fmr->ref_count);
  435. #endif
  436. spin_unlock_irqrestore(&pool->pool_lock, flags);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(ib_fmr_pool_unmap);