percpu_ida.c 8.2 KB

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