percpu_ida.c 9.4 KB

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