percpu_ida.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_cpu *tags)
  104. {
  105. int tag = -ENOSPC;
  106. spin_lock(&tags->lock);
  107. if (tags->nr_free)
  108. tag = tags->freelist[--tags->nr_free];
  109. spin_unlock(&tags->lock);
  110. return tag;
  111. }
  112. /**
  113. * percpu_ida_alloc - allocate a tag
  114. * @pool: pool to allocate from
  115. * @gfp: gfp flags
  116. *
  117. * Returns a tag - an integer in the range [0..nr_tags) (passed to
  118. * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
  119. *
  120. * Safe to be called from interrupt context (assuming it isn't passed
  121. * __GFP_WAIT, of course).
  122. *
  123. * @gfp indicates whether or not to wait until a free id is available (it's not
  124. * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
  125. * however long it takes until another thread frees an id (same semantics as a
  126. * mempool).
  127. *
  128. * Will not fail if passed __GFP_WAIT.
  129. */
  130. int percpu_ida_alloc(struct percpu_ida *pool, gfp_t gfp)
  131. {
  132. DEFINE_WAIT(wait);
  133. struct percpu_ida_cpu *tags;
  134. unsigned long flags;
  135. int tag;
  136. local_irq_save(flags);
  137. tags = this_cpu_ptr(pool->tag_cpu);
  138. /* Fastpath */
  139. tag = alloc_local_tag(tags);
  140. if (likely(tag >= 0)) {
  141. local_irq_restore(flags);
  142. return tag;
  143. }
  144. while (1) {
  145. spin_lock(&pool->lock);
  146. /*
  147. * prepare_to_wait() must come before steal_tags(), in case
  148. * percpu_ida_free() on another cpu flips a bit in
  149. * cpus_have_tags
  150. *
  151. * global lock held and irqs disabled, don't need percpu lock
  152. */
  153. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  154. if (!tags->nr_free)
  155. alloc_global_tags(pool, tags);
  156. if (!tags->nr_free)
  157. steal_tags(pool, tags);
  158. if (tags->nr_free) {
  159. tag = tags->freelist[--tags->nr_free];
  160. if (tags->nr_free)
  161. cpumask_set_cpu(smp_processor_id(),
  162. &pool->cpus_have_tags);
  163. }
  164. spin_unlock(&pool->lock);
  165. local_irq_restore(flags);
  166. if (tag >= 0 || !(gfp & __GFP_WAIT))
  167. break;
  168. schedule();
  169. local_irq_save(flags);
  170. tags = this_cpu_ptr(pool->tag_cpu);
  171. }
  172. finish_wait(&pool->wait, &wait);
  173. return tag;
  174. }
  175. EXPORT_SYMBOL_GPL(percpu_ida_alloc);
  176. /**
  177. * percpu_ida_free - free a tag
  178. * @pool: pool @tag was allocated from
  179. * @tag: a tag previously allocated with percpu_ida_alloc()
  180. *
  181. * Safe to be called from interrupt context.
  182. */
  183. void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  184. {
  185. struct percpu_ida_cpu *tags;
  186. unsigned long flags;
  187. unsigned nr_free;
  188. BUG_ON(tag >= pool->nr_tags);
  189. local_irq_save(flags);
  190. tags = this_cpu_ptr(pool->tag_cpu);
  191. spin_lock(&tags->lock);
  192. tags->freelist[tags->nr_free++] = tag;
  193. nr_free = tags->nr_free;
  194. spin_unlock(&tags->lock);
  195. if (nr_free == 1) {
  196. cpumask_set_cpu(smp_processor_id(),
  197. &pool->cpus_have_tags);
  198. wake_up(&pool->wait);
  199. }
  200. if (nr_free == pool->percpu_max_size) {
  201. spin_lock(&pool->lock);
  202. /*
  203. * Global lock held and irqs disabled, don't need percpu
  204. * lock
  205. */
  206. if (tags->nr_free == pool->percpu_max_size) {
  207. move_tags(pool->freelist, &pool->nr_free,
  208. tags->freelist, &tags->nr_free,
  209. pool->percpu_batch_size);
  210. wake_up(&pool->wait);
  211. }
  212. spin_unlock(&pool->lock);
  213. }
  214. local_irq_restore(flags);
  215. }
  216. EXPORT_SYMBOL_GPL(percpu_ida_free);
  217. /**
  218. * percpu_ida_destroy - release a tag pool's resources
  219. * @pool: pool to free
  220. *
  221. * Frees the resources allocated by percpu_ida_init().
  222. */
  223. void percpu_ida_destroy(struct percpu_ida *pool)
  224. {
  225. free_percpu(pool->tag_cpu);
  226. free_pages((unsigned long) pool->freelist,
  227. get_order(pool->nr_tags * sizeof(unsigned)));
  228. }
  229. EXPORT_SYMBOL_GPL(percpu_ida_destroy);
  230. /**
  231. * percpu_ida_init - initialize a percpu tag pool
  232. * @pool: pool to initialize
  233. * @nr_tags: number of tags that will be available for allocation
  234. *
  235. * Initializes @pool so that it can be used to allocate tags - integers in the
  236. * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
  237. * preallocated array of tag structures.
  238. *
  239. * Allocation is percpu, but sharding is limited by nr_tags - for best
  240. * performance, the workload should not span more cpus than nr_tags / 128.
  241. */
  242. int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
  243. unsigned long max_size, unsigned long batch_size)
  244. {
  245. unsigned i, cpu, order;
  246. memset(pool, 0, sizeof(*pool));
  247. init_waitqueue_head(&pool->wait);
  248. spin_lock_init(&pool->lock);
  249. pool->nr_tags = nr_tags;
  250. pool->percpu_max_size = max_size;
  251. pool->percpu_batch_size = batch_size;
  252. /* Guard against overflow */
  253. if (nr_tags > (unsigned) INT_MAX + 1) {
  254. pr_err("percpu_ida_init(): nr_tags too large\n");
  255. return -EINVAL;
  256. }
  257. order = get_order(nr_tags * sizeof(unsigned));
  258. pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
  259. if (!pool->freelist)
  260. return -ENOMEM;
  261. for (i = 0; i < nr_tags; i++)
  262. pool->freelist[i] = i;
  263. pool->nr_free = nr_tags;
  264. pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
  265. pool->percpu_max_size * sizeof(unsigned),
  266. sizeof(unsigned));
  267. if (!pool->tag_cpu)
  268. goto err;
  269. for_each_possible_cpu(cpu)
  270. spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
  271. return 0;
  272. err:
  273. percpu_ida_destroy(pool);
  274. return -ENOMEM;
  275. }
  276. EXPORT_SYMBOL_GPL(__percpu_ida_init);
  277. /**
  278. * percpu_ida_for_each_free - iterate free ids of a pool
  279. * @pool: pool to iterate
  280. * @fn: interate callback function
  281. * @data: parameter for @fn
  282. *
  283. * Note, this doesn't guarantee to iterate all free ids restrictly. Some free
  284. * ids might be missed, some might be iterated duplicated, and some might
  285. * be iterated and not free soon.
  286. */
  287. int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  288. void *data)
  289. {
  290. unsigned long flags;
  291. struct percpu_ida_cpu *remote;
  292. unsigned cpu, i, err = 0;
  293. local_irq_save(flags);
  294. for_each_possible_cpu(cpu) {
  295. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  296. spin_lock(&remote->lock);
  297. for (i = 0; i < remote->nr_free; i++) {
  298. err = fn(remote->freelist[i], data);
  299. if (err)
  300. break;
  301. }
  302. spin_unlock(&remote->lock);
  303. if (err)
  304. goto out;
  305. }
  306. spin_lock(&pool->lock);
  307. for (i = 0; i < pool->nr_free; i++) {
  308. err = fn(pool->freelist[i], data);
  309. if (err)
  310. break;
  311. }
  312. spin_unlock(&pool->lock);
  313. out:
  314. local_irq_restore(flags);
  315. return err;
  316. }
  317. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  318. /**
  319. * percpu_ida_free_tags - return free tags number of a specific cpu or global pool
  320. * @pool: pool related
  321. * @cpu: specific cpu or global pool if @cpu == nr_cpu_ids
  322. *
  323. * Note: this just returns a snapshot of free tags number.
  324. */
  325. unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
  326. {
  327. struct percpu_ida_cpu *remote;
  328. if (cpu == nr_cpu_ids)
  329. return pool->nr_free;
  330. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  331. return remote->nr_free;
  332. }
  333. EXPORT_SYMBOL_GPL(percpu_ida_free_tags);