rxe_pool.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, 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 "rxe.h"
  34. #include "rxe_loc.h"
  35. /* info about object pools
  36. * note that mr and mw share a single index space
  37. * so that one can map an lkey to the correct type of object
  38. */
  39. struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = {
  40. [RXE_TYPE_UC] = {
  41. .name = "rxe-uc",
  42. .size = sizeof(struct rxe_ucontext),
  43. },
  44. [RXE_TYPE_PD] = {
  45. .name = "rxe-pd",
  46. .size = sizeof(struct rxe_pd),
  47. },
  48. [RXE_TYPE_AH] = {
  49. .name = "rxe-ah",
  50. .size = sizeof(struct rxe_ah),
  51. .flags = RXE_POOL_ATOMIC,
  52. },
  53. [RXE_TYPE_SRQ] = {
  54. .name = "rxe-srq",
  55. .size = sizeof(struct rxe_srq),
  56. .flags = RXE_POOL_INDEX,
  57. .min_index = RXE_MIN_SRQ_INDEX,
  58. .max_index = RXE_MAX_SRQ_INDEX,
  59. },
  60. [RXE_TYPE_QP] = {
  61. .name = "rxe-qp",
  62. .size = sizeof(struct rxe_qp),
  63. .cleanup = rxe_qp_cleanup,
  64. .flags = RXE_POOL_INDEX,
  65. .min_index = RXE_MIN_QP_INDEX,
  66. .max_index = RXE_MAX_QP_INDEX,
  67. },
  68. [RXE_TYPE_CQ] = {
  69. .name = "rxe-cq",
  70. .size = sizeof(struct rxe_cq),
  71. .cleanup = rxe_cq_cleanup,
  72. },
  73. [RXE_TYPE_MR] = {
  74. .name = "rxe-mr",
  75. .size = sizeof(struct rxe_mem),
  76. .cleanup = rxe_mem_cleanup,
  77. .flags = RXE_POOL_INDEX,
  78. .max_index = RXE_MAX_MR_INDEX,
  79. .min_index = RXE_MIN_MR_INDEX,
  80. },
  81. [RXE_TYPE_MW] = {
  82. .name = "rxe-mw",
  83. .size = sizeof(struct rxe_mem),
  84. .flags = RXE_POOL_INDEX,
  85. .max_index = RXE_MAX_MW_INDEX,
  86. .min_index = RXE_MIN_MW_INDEX,
  87. },
  88. [RXE_TYPE_MC_GRP] = {
  89. .name = "rxe-mc_grp",
  90. .size = sizeof(struct rxe_mc_grp),
  91. .cleanup = rxe_mc_cleanup,
  92. .flags = RXE_POOL_KEY,
  93. .key_offset = offsetof(struct rxe_mc_grp, mgid),
  94. .key_size = sizeof(union ib_gid),
  95. },
  96. [RXE_TYPE_MC_ELEM] = {
  97. .name = "rxe-mc_elem",
  98. .size = sizeof(struct rxe_mc_elem),
  99. .flags = RXE_POOL_ATOMIC,
  100. },
  101. };
  102. static inline char *pool_name(struct rxe_pool *pool)
  103. {
  104. return rxe_type_info[pool->type].name;
  105. }
  106. static inline struct kmem_cache *pool_cache(struct rxe_pool *pool)
  107. {
  108. return rxe_type_info[pool->type].cache;
  109. }
  110. static inline enum rxe_elem_type rxe_type(void *arg)
  111. {
  112. struct rxe_pool_entry *elem = arg;
  113. return elem->pool->type;
  114. }
  115. int rxe_cache_init(void)
  116. {
  117. int err;
  118. int i;
  119. size_t size;
  120. struct rxe_type_info *type;
  121. for (i = 0; i < RXE_NUM_TYPES; i++) {
  122. type = &rxe_type_info[i];
  123. size = ALIGN(type->size, RXE_POOL_ALIGN);
  124. type->cache = kmem_cache_create(type->name, size,
  125. RXE_POOL_ALIGN,
  126. RXE_POOL_CACHE_FLAGS, NULL);
  127. if (!type->cache) {
  128. pr_err("Unable to init kmem cache for %s\n",
  129. type->name);
  130. err = -ENOMEM;
  131. goto err1;
  132. }
  133. }
  134. return 0;
  135. err1:
  136. while (--i >= 0) {
  137. kmem_cache_destroy(type->cache);
  138. type->cache = NULL;
  139. }
  140. return err;
  141. }
  142. void rxe_cache_exit(void)
  143. {
  144. int i;
  145. struct rxe_type_info *type;
  146. for (i = 0; i < RXE_NUM_TYPES; i++) {
  147. type = &rxe_type_info[i];
  148. kmem_cache_destroy(type->cache);
  149. type->cache = NULL;
  150. }
  151. }
  152. static int rxe_pool_init_index(struct rxe_pool *pool, u32 max, u32 min)
  153. {
  154. int err = 0;
  155. size_t size;
  156. if ((max - min + 1) < pool->max_elem) {
  157. pr_warn("not enough indices for max_elem\n");
  158. err = -EINVAL;
  159. goto out;
  160. }
  161. pool->max_index = max;
  162. pool->min_index = min;
  163. size = BITS_TO_LONGS(max - min + 1) * sizeof(long);
  164. pool->table = kmalloc(size, GFP_KERNEL);
  165. if (!pool->table) {
  166. pr_warn("no memory for bit table\n");
  167. err = -ENOMEM;
  168. goto out;
  169. }
  170. pool->table_size = size;
  171. bitmap_zero(pool->table, max - min + 1);
  172. out:
  173. return err;
  174. }
  175. int rxe_pool_init(
  176. struct rxe_dev *rxe,
  177. struct rxe_pool *pool,
  178. enum rxe_elem_type type,
  179. unsigned max_elem)
  180. {
  181. int err = 0;
  182. size_t size = rxe_type_info[type].size;
  183. memset(pool, 0, sizeof(*pool));
  184. pool->rxe = rxe;
  185. pool->type = type;
  186. pool->max_elem = max_elem;
  187. pool->elem_size = ALIGN(size, RXE_POOL_ALIGN);
  188. pool->flags = rxe_type_info[type].flags;
  189. pool->tree = RB_ROOT;
  190. pool->cleanup = rxe_type_info[type].cleanup;
  191. atomic_set(&pool->num_elem, 0);
  192. kref_init(&pool->ref_cnt);
  193. spin_lock_init(&pool->pool_lock);
  194. if (rxe_type_info[type].flags & RXE_POOL_INDEX) {
  195. err = rxe_pool_init_index(pool,
  196. rxe_type_info[type].max_index,
  197. rxe_type_info[type].min_index);
  198. if (err)
  199. goto out;
  200. }
  201. if (rxe_type_info[type].flags & RXE_POOL_KEY) {
  202. pool->key_offset = rxe_type_info[type].key_offset;
  203. pool->key_size = rxe_type_info[type].key_size;
  204. }
  205. pool->state = rxe_pool_valid;
  206. out:
  207. return err;
  208. }
  209. static void rxe_pool_release(struct kref *kref)
  210. {
  211. struct rxe_pool *pool = container_of(kref, struct rxe_pool, ref_cnt);
  212. pool->state = rxe_pool_invalid;
  213. kfree(pool->table);
  214. }
  215. static void rxe_pool_put(struct rxe_pool *pool)
  216. {
  217. kref_put(&pool->ref_cnt, rxe_pool_release);
  218. }
  219. int rxe_pool_cleanup(struct rxe_pool *pool)
  220. {
  221. unsigned long flags;
  222. spin_lock_irqsave(&pool->pool_lock, flags);
  223. pool->state = rxe_pool_invalid;
  224. if (atomic_read(&pool->num_elem) > 0)
  225. pr_warn("%s pool destroyed with unfree'd elem\n",
  226. pool_name(pool));
  227. spin_unlock_irqrestore(&pool->pool_lock, flags);
  228. rxe_pool_put(pool);
  229. return 0;
  230. }
  231. static u32 alloc_index(struct rxe_pool *pool)
  232. {
  233. u32 index;
  234. u32 range = pool->max_index - pool->min_index + 1;
  235. index = find_next_zero_bit(pool->table, range, pool->last);
  236. if (index >= range)
  237. index = find_first_zero_bit(pool->table, range);
  238. WARN_ON_ONCE(index >= range);
  239. set_bit(index, pool->table);
  240. pool->last = index;
  241. return index + pool->min_index;
  242. }
  243. static void insert_index(struct rxe_pool *pool, struct rxe_pool_entry *new)
  244. {
  245. struct rb_node **link = &pool->tree.rb_node;
  246. struct rb_node *parent = NULL;
  247. struct rxe_pool_entry *elem;
  248. while (*link) {
  249. parent = *link;
  250. elem = rb_entry(parent, struct rxe_pool_entry, node);
  251. if (elem->index == new->index) {
  252. pr_warn("element already exists!\n");
  253. goto out;
  254. }
  255. if (elem->index > new->index)
  256. link = &(*link)->rb_left;
  257. else
  258. link = &(*link)->rb_right;
  259. }
  260. rb_link_node(&new->node, parent, link);
  261. rb_insert_color(&new->node, &pool->tree);
  262. out:
  263. return;
  264. }
  265. static void insert_key(struct rxe_pool *pool, struct rxe_pool_entry *new)
  266. {
  267. struct rb_node **link = &pool->tree.rb_node;
  268. struct rb_node *parent = NULL;
  269. struct rxe_pool_entry *elem;
  270. int cmp;
  271. while (*link) {
  272. parent = *link;
  273. elem = rb_entry(parent, struct rxe_pool_entry, node);
  274. cmp = memcmp((u8 *)elem + pool->key_offset,
  275. (u8 *)new + pool->key_offset, pool->key_size);
  276. if (cmp == 0) {
  277. pr_warn("key already exists!\n");
  278. goto out;
  279. }
  280. if (cmp > 0)
  281. link = &(*link)->rb_left;
  282. else
  283. link = &(*link)->rb_right;
  284. }
  285. rb_link_node(&new->node, parent, link);
  286. rb_insert_color(&new->node, &pool->tree);
  287. out:
  288. return;
  289. }
  290. void rxe_add_key(void *arg, void *key)
  291. {
  292. struct rxe_pool_entry *elem = arg;
  293. struct rxe_pool *pool = elem->pool;
  294. unsigned long flags;
  295. spin_lock_irqsave(&pool->pool_lock, flags);
  296. memcpy((u8 *)elem + pool->key_offset, key, pool->key_size);
  297. insert_key(pool, elem);
  298. spin_unlock_irqrestore(&pool->pool_lock, flags);
  299. }
  300. void rxe_drop_key(void *arg)
  301. {
  302. struct rxe_pool_entry *elem = arg;
  303. struct rxe_pool *pool = elem->pool;
  304. unsigned long flags;
  305. spin_lock_irqsave(&pool->pool_lock, flags);
  306. rb_erase(&elem->node, &pool->tree);
  307. spin_unlock_irqrestore(&pool->pool_lock, flags);
  308. }
  309. void rxe_add_index(void *arg)
  310. {
  311. struct rxe_pool_entry *elem = arg;
  312. struct rxe_pool *pool = elem->pool;
  313. unsigned long flags;
  314. spin_lock_irqsave(&pool->pool_lock, flags);
  315. elem->index = alloc_index(pool);
  316. insert_index(pool, elem);
  317. spin_unlock_irqrestore(&pool->pool_lock, flags);
  318. }
  319. void rxe_drop_index(void *arg)
  320. {
  321. struct rxe_pool_entry *elem = arg;
  322. struct rxe_pool *pool = elem->pool;
  323. unsigned long flags;
  324. spin_lock_irqsave(&pool->pool_lock, flags);
  325. clear_bit(elem->index - pool->min_index, pool->table);
  326. rb_erase(&elem->node, &pool->tree);
  327. spin_unlock_irqrestore(&pool->pool_lock, flags);
  328. }
  329. void *rxe_alloc(struct rxe_pool *pool)
  330. {
  331. struct rxe_pool_entry *elem;
  332. unsigned long flags;
  333. might_sleep_if(!(pool->flags & RXE_POOL_ATOMIC));
  334. spin_lock_irqsave(&pool->pool_lock, flags);
  335. if (pool->state != rxe_pool_valid) {
  336. spin_unlock_irqrestore(&pool->pool_lock, flags);
  337. return NULL;
  338. }
  339. kref_get(&pool->ref_cnt);
  340. spin_unlock_irqrestore(&pool->pool_lock, flags);
  341. kref_get(&pool->rxe->ref_cnt);
  342. if (atomic_inc_return(&pool->num_elem) > pool->max_elem) {
  343. atomic_dec(&pool->num_elem);
  344. rxe_dev_put(pool->rxe);
  345. rxe_pool_put(pool);
  346. return NULL;
  347. }
  348. elem = kmem_cache_zalloc(pool_cache(pool),
  349. (pool->flags & RXE_POOL_ATOMIC) ?
  350. GFP_ATOMIC : GFP_KERNEL);
  351. if (!elem)
  352. return NULL;
  353. elem->pool = pool;
  354. kref_init(&elem->ref_cnt);
  355. return elem;
  356. }
  357. void rxe_elem_release(struct kref *kref)
  358. {
  359. struct rxe_pool_entry *elem =
  360. container_of(kref, struct rxe_pool_entry, ref_cnt);
  361. struct rxe_pool *pool = elem->pool;
  362. if (pool->cleanup)
  363. pool->cleanup(elem);
  364. kmem_cache_free(pool_cache(pool), elem);
  365. atomic_dec(&pool->num_elem);
  366. rxe_dev_put(pool->rxe);
  367. rxe_pool_put(pool);
  368. }
  369. void *rxe_pool_get_index(struct rxe_pool *pool, u32 index)
  370. {
  371. struct rb_node *node = NULL;
  372. struct rxe_pool_entry *elem = NULL;
  373. unsigned long flags;
  374. spin_lock_irqsave(&pool->pool_lock, flags);
  375. if (pool->state != rxe_pool_valid)
  376. goto out;
  377. node = pool->tree.rb_node;
  378. while (node) {
  379. elem = rb_entry(node, struct rxe_pool_entry, node);
  380. if (elem->index > index)
  381. node = node->rb_left;
  382. else if (elem->index < index)
  383. node = node->rb_right;
  384. else
  385. break;
  386. }
  387. if (node)
  388. kref_get(&elem->ref_cnt);
  389. out:
  390. spin_unlock_irqrestore(&pool->pool_lock, flags);
  391. return node ? (void *)elem : NULL;
  392. }
  393. void *rxe_pool_get_key(struct rxe_pool *pool, void *key)
  394. {
  395. struct rb_node *node = NULL;
  396. struct rxe_pool_entry *elem = NULL;
  397. int cmp;
  398. unsigned long flags;
  399. spin_lock_irqsave(&pool->pool_lock, flags);
  400. if (pool->state != rxe_pool_valid)
  401. goto out;
  402. node = pool->tree.rb_node;
  403. while (node) {
  404. elem = rb_entry(node, struct rxe_pool_entry, node);
  405. cmp = memcmp((u8 *)elem + pool->key_offset,
  406. key, pool->key_size);
  407. if (cmp > 0)
  408. node = node->rb_left;
  409. else if (cmp < 0)
  410. node = node->rb_right;
  411. else
  412. break;
  413. }
  414. if (node)
  415. kref_get(&elem->ref_cnt);
  416. out:
  417. spin_unlock_irqrestore(&pool->pool_lock, flags);
  418. return node ? ((void *)elem) : NULL;
  419. }