percpu_ida.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. * @state: task state for prepare_to_wait
  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. * TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 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 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE.
  126. */
  127. int percpu_ida_alloc(struct percpu_ida *pool, int state)
  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. if (state != TASK_RUNNING)
  151. prepare_to_wait(&pool->wait, &wait, state);
  152. if (!tags->nr_free)
  153. alloc_global_tags(pool, tags);
  154. if (!tags->nr_free)
  155. steal_tags(pool, tags);
  156. if (tags->nr_free) {
  157. tag = tags->freelist[--tags->nr_free];
  158. if (tags->nr_free)
  159. cpumask_set_cpu(smp_processor_id(),
  160. &pool->cpus_have_tags);
  161. }
  162. spin_unlock(&pool->lock);
  163. local_irq_restore(flags);
  164. if (tag >= 0 || state == TASK_RUNNING)
  165. break;
  166. if (signal_pending_state(state, current)) {
  167. tag = -ERESTARTSYS;
  168. break;
  169. }
  170. schedule();
  171. local_irq_save(flags);
  172. tags = this_cpu_ptr(pool->tag_cpu);
  173. }
  174. if (state != TASK_RUNNING)
  175. finish_wait(&pool->wait, &wait);
  176. return tag;
  177. }
  178. EXPORT_SYMBOL_GPL(percpu_ida_alloc);
  179. /**
  180. * percpu_ida_free - free a tag
  181. * @pool: pool @tag was allocated from
  182. * @tag: a tag previously allocated with percpu_ida_alloc()
  183. *
  184. * Safe to be called from interrupt context.
  185. */
  186. void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  187. {
  188. struct percpu_ida_cpu *tags;
  189. unsigned long flags;
  190. unsigned nr_free;
  191. BUG_ON(tag >= pool->nr_tags);
  192. local_irq_save(flags);
  193. tags = this_cpu_ptr(pool->tag_cpu);
  194. spin_lock(&tags->lock);
  195. tags->freelist[tags->nr_free++] = tag;
  196. nr_free = tags->nr_free;
  197. spin_unlock(&tags->lock);
  198. if (nr_free == 1) {
  199. cpumask_set_cpu(smp_processor_id(),
  200. &pool->cpus_have_tags);
  201. wake_up(&pool->wait);
  202. }
  203. if (nr_free == pool->percpu_max_size) {
  204. spin_lock(&pool->lock);
  205. /*
  206. * Global lock held and irqs disabled, don't need percpu
  207. * lock
  208. */
  209. if (tags->nr_free == pool->percpu_max_size) {
  210. move_tags(pool->freelist, &pool->nr_free,
  211. tags->freelist, &tags->nr_free,
  212. pool->percpu_batch_size);
  213. wake_up(&pool->wait);
  214. }
  215. spin_unlock(&pool->lock);
  216. }
  217. local_irq_restore(flags);
  218. }
  219. EXPORT_SYMBOL_GPL(percpu_ida_free);
  220. /**
  221. * percpu_ida_destroy - release a tag pool's resources
  222. * @pool: pool to free
  223. *
  224. * Frees the resources allocated by percpu_ida_init().
  225. */
  226. void percpu_ida_destroy(struct percpu_ida *pool)
  227. {
  228. free_percpu(pool->tag_cpu);
  229. free_pages((unsigned long) pool->freelist,
  230. get_order(pool->nr_tags * sizeof(unsigned)));
  231. }
  232. EXPORT_SYMBOL_GPL(percpu_ida_destroy);
  233. /**
  234. * percpu_ida_init - initialize a percpu tag pool
  235. * @pool: pool to initialize
  236. * @nr_tags: number of tags that will be available for allocation
  237. *
  238. * Initializes @pool so that it can be used to allocate tags - integers in the
  239. * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
  240. * preallocated array of tag structures.
  241. *
  242. * Allocation is percpu, but sharding is limited by nr_tags - for best
  243. * performance, the workload should not span more cpus than nr_tags / 128.
  244. */
  245. int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
  246. unsigned long max_size, unsigned long batch_size)
  247. {
  248. unsigned i, cpu, order;
  249. memset(pool, 0, sizeof(*pool));
  250. init_waitqueue_head(&pool->wait);
  251. spin_lock_init(&pool->lock);
  252. pool->nr_tags = nr_tags;
  253. pool->percpu_max_size = max_size;
  254. pool->percpu_batch_size = batch_size;
  255. /* Guard against overflow */
  256. if (nr_tags > (unsigned) INT_MAX + 1) {
  257. pr_err("percpu_ida_init(): nr_tags too large\n");
  258. return -EINVAL;
  259. }
  260. order = get_order(nr_tags * sizeof(unsigned));
  261. pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
  262. if (!pool->freelist)
  263. return -ENOMEM;
  264. for (i = 0; i < nr_tags; i++)
  265. pool->freelist[i] = i;
  266. pool->nr_free = nr_tags;
  267. pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
  268. pool->percpu_max_size * sizeof(unsigned),
  269. sizeof(unsigned));
  270. if (!pool->tag_cpu)
  271. goto err;
  272. for_each_possible_cpu(cpu)
  273. spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
  274. return 0;
  275. err:
  276. percpu_ida_destroy(pool);
  277. return -ENOMEM;
  278. }
  279. EXPORT_SYMBOL_GPL(__percpu_ida_init);
  280. /**
  281. * percpu_ida_for_each_free - iterate free ids of a pool
  282. * @pool: pool to iterate
  283. * @fn: interate callback function
  284. * @data: parameter for @fn
  285. *
  286. * Note, this doesn't guarantee to iterate all free ids restrictly. Some free
  287. * ids might be missed, some might be iterated duplicated, and some might
  288. * be iterated and not free soon.
  289. */
  290. int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  291. void *data)
  292. {
  293. unsigned long flags;
  294. struct percpu_ida_cpu *remote;
  295. unsigned cpu, i, err = 0;
  296. local_irq_save(flags);
  297. for_each_possible_cpu(cpu) {
  298. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  299. spin_lock(&remote->lock);
  300. for (i = 0; i < remote->nr_free; i++) {
  301. err = fn(remote->freelist[i], data);
  302. if (err)
  303. break;
  304. }
  305. spin_unlock(&remote->lock);
  306. if (err)
  307. goto out;
  308. }
  309. spin_lock(&pool->lock);
  310. for (i = 0; i < pool->nr_free; i++) {
  311. err = fn(pool->freelist[i], data);
  312. if (err)
  313. break;
  314. }
  315. spin_unlock(&pool->lock);
  316. out:
  317. local_irq_restore(flags);
  318. return err;
  319. }
  320. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  321. /**
  322. * percpu_ida_free_tags - return free tags number of a specific cpu or global pool
  323. * @pool: pool related
  324. * @cpu: specific cpu or global pool if @cpu == nr_cpu_ids
  325. *
  326. * Note: this just returns a snapshot of free tags number.
  327. */
  328. unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
  329. {
  330. struct percpu_ida_cpu *remote;
  331. if (cpu == nr_cpu_ids)
  332. return pool->nr_free;
  333. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  334. return remote->nr_free;
  335. }
  336. EXPORT_SYMBOL_GPL(percpu_ida_free_tags);