fmr_pool.c 14 KB

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