percpu_ida.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Percpu IDA library
  3. *
  4. * Copyright (C) 2013 Datera, Inc. Kent Overstreet
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/bitmap.h>
  17. #include <linux/bitops.h>
  18. #include <linux/bug.h>
  19. #include <linux/err.h>
  20. #include <linux/export.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/idr.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/percpu.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/percpu_ida.h>
  31. struct percpu_ida_cpu {
  32. /*
  33. * Even though this is percpu, we need a lock for tag stealing by remote
  34. * CPUs:
  35. */
  36. spinlock_t lock;
  37. /* nr_free/freelist form a stack of free IDs */
  38. unsigned nr_free;
  39. unsigned freelist[];
  40. };
  41. static inline void move_tags(unsigned *dst, unsigned *dst_nr,
  42. unsigned *src, unsigned *src_nr,
  43. unsigned nr)
  44. {
  45. *src_nr -= nr;
  46. memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
  47. *dst_nr += nr;
  48. }
  49. /*
  50. * Try to steal tags from a remote cpu's percpu freelist.
  51. *
  52. * We first check how many percpu freelists have tags - we don't steal tags
  53. * unless enough percpu freelists have tags on them that it's possible more than
  54. * half the total tags could be stuck on remote percpu freelists.
  55. *
  56. * Then we iterate through the cpus until we find some tags - we don't attempt
  57. * to find the "best" cpu to steal from, to keep cacheline bouncing to a
  58. * minimum.
  59. */
  60. static inline void steal_tags(struct percpu_ida *pool,
  61. struct percpu_ida_cpu *tags)
  62. {
  63. unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
  64. struct percpu_ida_cpu *remote;
  65. for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
  66. cpus_have_tags * pool->percpu_max_size > pool->nr_tags / 2;
  67. cpus_have_tags--) {
  68. cpu = cpumask_next(cpu, &pool->cpus_have_tags);
  69. if (cpu >= nr_cpu_ids) {
  70. cpu = cpumask_first(&pool->cpus_have_tags);
  71. if (cpu >= nr_cpu_ids)
  72. BUG();
  73. }
  74. pool->cpu_last_stolen = cpu;
  75. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  76. cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
  77. if (remote == tags)
  78. continue;
  79. spin_lock(&remote->lock);
  80. if (remote->nr_free) {
  81. memcpy(tags->freelist,
  82. remote->freelist,
  83. sizeof(unsigned) * remote->nr_free);
  84. tags->nr_free = remote->nr_free;
  85. remote->nr_free = 0;
  86. }
  87. spin_unlock(&remote->lock);
  88. if (tags->nr_free)
  89. break;
  90. }
  91. }
  92. /*
  93. * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
  94. * our percpu freelist:
  95. */
  96. static inline void alloc_global_tags(struct percpu_ida *pool,
  97. struct percpu_ida_cpu *tags)
  98. {
  99. move_tags(tags->freelist, &tags->nr_free,
  100. pool->freelist, &pool->nr_free,
  101. min(pool->nr_free, pool->percpu_batch_size));
  102. }
  103. static inline unsigned alloc_local_tag(struct percpu_ida *pool,
  104. struct percpu_ida_cpu *tags)
  105. {
  106. int tag = -ENOSPC;
  107. spin_lock(&tags->lock);
  108. if (tags->nr_free)
  109. tag = tags->freelist[--tags->nr_free];
  110. spin_unlock(&tags->lock);
  111. return tag;
  112. }
  113. /**
  114. * percpu_ida_alloc - allocate a tag
  115. * @pool: pool to allocate from
  116. * @gfp: gfp flags
  117. *
  118. * Returns a tag - an integer in the range [0..nr_tags) (passed to
  119. * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
  120. *
  121. * Safe to be called from interrupt context (assuming it isn't passed
  122. * __GFP_WAIT, of course).
  123. *
  124. * @gfp indicates whether or not to wait until a free id is available (it's not
  125. * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
  126. * however long it takes until another thread frees an id (same semantics as a
  127. * mempool).
  128. *
  129. * Will not fail if passed __GFP_WAIT.
  130. */
  131. int percpu_ida_alloc(struct percpu_ida *pool, gfp_t gfp)
  132. {
  133. DEFINE_WAIT(wait);
  134. struct percpu_ida_cpu *tags;
  135. unsigned long flags;
  136. int tag;
  137. local_irq_save(flags);
  138. tags = this_cpu_ptr(pool->tag_cpu);
  139. /* Fastpath */
  140. tag = alloc_local_tag(pool, tags);
  141. if (likely(tag >= 0)) {
  142. local_irq_restore(flags);
  143. return tag;
  144. }
  145. while (1) {
  146. spin_lock(&pool->lock);
  147. /*
  148. * prepare_to_wait() must come before steal_tags(), in case
  149. * percpu_ida_free() on another cpu flips a bit in
  150. * cpus_have_tags
  151. *
  152. * global lock held and irqs disabled, don't need percpu lock
  153. */
  154. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  155. if (!tags->nr_free)
  156. alloc_global_tags(pool, tags);
  157. if (!tags->nr_free)
  158. steal_tags(pool, tags);
  159. if (tags->nr_free) {
  160. tag = tags->freelist[--tags->nr_free];
  161. if (tags->nr_free)
  162. cpumask_set_cpu(smp_processor_id(),
  163. &pool->cpus_have_tags);
  164. }
  165. spin_unlock(&pool->lock);
  166. local_irq_restore(flags);
  167. if (tag >= 0 || !(gfp & __GFP_WAIT))
  168. break;
  169. schedule();
  170. local_irq_save(flags);
  171. tags = this_cpu_ptr(pool->tag_cpu);
  172. }
  173. finish_wait(&pool->wait, &wait);
  174. return tag;
  175. }
  176. EXPORT_SYMBOL_GPL(percpu_ida_alloc);
  177. /**
  178. * percpu_ida_free - free a tag
  179. * @pool: pool @tag was allocated from
  180. * @tag: a tag previously allocated with percpu_ida_alloc()
  181. *
  182. * Safe to be called from interrupt context.
  183. */
  184. void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  185. {
  186. struct percpu_ida_cpu *tags;
  187. unsigned long flags;
  188. unsigned nr_free;
  189. BUG_ON(tag >= pool->nr_tags);
  190. local_irq_save(flags);
  191. tags = this_cpu_ptr(pool->tag_cpu);
  192. spin_lock(&tags->lock);
  193. tags->freelist[tags->nr_free++] = tag;
  194. nr_free = tags->nr_free;
  195. spin_unlock(&tags->lock);
  196. if (nr_free == 1) {
  197. cpumask_set_cpu(smp_processor_id(),
  198. &pool->cpus_have_tags);
  199. wake_up(&pool->wait);
  200. }
  201. if (nr_free == pool->percpu_max_size) {
  202. spin_lock(&pool->lock);
  203. /*
  204. * Global lock held and irqs disabled, don't need percpu
  205. * lock
  206. */
  207. if (tags->nr_free == pool->percpu_max_size) {
  208. move_tags(pool->freelist, &pool->nr_free,
  209. tags->freelist, &tags->nr_free,
  210. pool->percpu_batch_size);
  211. wake_up(&pool->wait);
  212. }
  213. spin_unlock(&pool->lock);
  214. }
  215. local_irq_restore(flags);
  216. }
  217. EXPORT_SYMBOL_GPL(percpu_ida_free);
  218. /**
  219. * percpu_ida_destroy - release a tag pool's resources
  220. * @pool: pool to free
  221. *
  222. * Frees the resources allocated by percpu_ida_init().
  223. */
  224. void percpu_ida_destroy(struct percpu_ida *pool)
  225. {
  226. free_percpu(pool->tag_cpu);
  227. free_pages((unsigned long) pool->freelist,
  228. get_order(pool->nr_tags * sizeof(unsigned)));
  229. }
  230. EXPORT_SYMBOL_GPL(percpu_ida_destroy);
  231. /**
  232. * percpu_ida_init - initialize a percpu tag pool
  233. * @pool: pool to initialize
  234. * @nr_tags: number of tags that will be available for allocation
  235. *
  236. * Initializes @pool so that it can be used to allocate tags - integers in the
  237. * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
  238. * preallocated array of tag structures.
  239. *
  240. * Allocation is percpu, but sharding is limited by nr_tags - for best
  241. * performance, the workload should not span more cpus than nr_tags / 128.
  242. */
  243. int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
  244. unsigned long max_size, unsigned long batch_size)
  245. {
  246. unsigned i, cpu, order;
  247. memset(pool, 0, sizeof(*pool));
  248. init_waitqueue_head(&pool->wait);
  249. spin_lock_init(&pool->lock);
  250. pool->nr_tags = nr_tags;
  251. pool->percpu_max_size = max_size;
  252. pool->percpu_batch_size = batch_size;
  253. /* Guard against overflow */
  254. if (nr_tags > (unsigned) INT_MAX + 1) {
  255. pr_err("percpu_ida_init(): nr_tags too large\n");
  256. return -EINVAL;
  257. }
  258. order = get_order(nr_tags * sizeof(unsigned));
  259. pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
  260. if (!pool->freelist)
  261. return -ENOMEM;
  262. for (i = 0; i < nr_tags; i++)
  263. pool->freelist[i] = i;
  264. pool->nr_free = nr_tags;
  265. pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
  266. pool->percpu_max_size * sizeof(unsigned),
  267. sizeof(unsigned));
  268. if (!pool->tag_cpu)
  269. goto err;
  270. for_each_possible_cpu(cpu)
  271. spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
  272. return 0;
  273. err:
  274. percpu_ida_destroy(pool);
  275. return -ENOMEM;
  276. }
  277. EXPORT_SYMBOL_GPL(__percpu_ida_init);