percpu_ida.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/percpu.h>
  24. #include <linux/sched.h>
  25. #include <linux/string.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/percpu_ida.h>
  28. struct percpu_ida_cpu {
  29. /*
  30. * Even though this is percpu, we need a lock for tag stealing by remote
  31. * CPUs:
  32. */
  33. spinlock_t lock;
  34. /* nr_free/freelist form a stack of free IDs */
  35. unsigned nr_free;
  36. unsigned freelist[];
  37. };
  38. static inline void move_tags(unsigned *dst, unsigned *dst_nr,
  39. unsigned *src, unsigned *src_nr,
  40. unsigned nr)
  41. {
  42. *src_nr -= nr;
  43. memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
  44. *dst_nr += nr;
  45. }
  46. /*
  47. * Try to steal tags from a remote cpu's percpu freelist.
  48. *
  49. * We first check how many percpu freelists have tags
  50. *
  51. * Then we iterate through the cpus until we find some tags - we don't attempt
  52. * to find the "best" cpu to steal from, to keep cacheline bouncing to a
  53. * minimum.
  54. */
  55. static inline void steal_tags(struct percpu_ida *pool,
  56. struct percpu_ida_cpu *tags)
  57. {
  58. unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
  59. struct percpu_ida_cpu *remote;
  60. for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
  61. cpus_have_tags; cpus_have_tags--) {
  62. cpu = cpumask_next(cpu, &pool->cpus_have_tags);
  63. if (cpu >= nr_cpu_ids) {
  64. cpu = cpumask_first(&pool->cpus_have_tags);
  65. if (cpu >= nr_cpu_ids)
  66. BUG();
  67. }
  68. pool->cpu_last_stolen = cpu;
  69. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  70. cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
  71. if (remote == tags)
  72. continue;
  73. spin_lock(&remote->lock);
  74. if (remote->nr_free) {
  75. memcpy(tags->freelist,
  76. remote->freelist,
  77. sizeof(unsigned) * remote->nr_free);
  78. tags->nr_free = remote->nr_free;
  79. remote->nr_free = 0;
  80. }
  81. spin_unlock(&remote->lock);
  82. if (tags->nr_free)
  83. break;
  84. }
  85. }
  86. /*
  87. * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
  88. * our percpu freelist:
  89. */
  90. static inline void alloc_global_tags(struct percpu_ida *pool,
  91. struct percpu_ida_cpu *tags)
  92. {
  93. move_tags(tags->freelist, &tags->nr_free,
  94. pool->freelist, &pool->nr_free,
  95. min(pool->nr_free, pool->percpu_batch_size));
  96. }
  97. static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
  98. {
  99. int tag = -ENOSPC;
  100. spin_lock(&tags->lock);
  101. if (tags->nr_free)
  102. tag = tags->freelist[--tags->nr_free];
  103. spin_unlock(&tags->lock);
  104. return tag;
  105. }
  106. /**
  107. * percpu_ida_alloc - allocate a tag
  108. * @pool: pool to allocate from
  109. * @state: task state for prepare_to_wait
  110. *
  111. * Returns a tag - an integer in the range [0..nr_tags) (passed to
  112. * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
  113. *
  114. * Safe to be called from interrupt context (assuming it isn't passed
  115. * TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, of course).
  116. *
  117. * @gfp indicates whether or not to wait until a free id is available (it's not
  118. * used for internal memory allocations); thus if passed __GFP_RECLAIM we may sleep
  119. * however long it takes until another thread frees an id (same semantics as a
  120. * mempool).
  121. *
  122. * Will not fail if passed TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE.
  123. */
  124. int percpu_ida_alloc(struct percpu_ida *pool, int state)
  125. {
  126. DEFINE_WAIT(wait);
  127. struct percpu_ida_cpu *tags;
  128. unsigned long flags;
  129. int tag;
  130. local_irq_save(flags);
  131. tags = this_cpu_ptr(pool->tag_cpu);
  132. /* Fastpath */
  133. tag = alloc_local_tag(tags);
  134. if (likely(tag >= 0)) {
  135. local_irq_restore(flags);
  136. return tag;
  137. }
  138. while (1) {
  139. spin_lock(&pool->lock);
  140. /*
  141. * prepare_to_wait() must come before steal_tags(), in case
  142. * percpu_ida_free() on another cpu flips a bit in
  143. * cpus_have_tags
  144. *
  145. * global lock held and irqs disabled, don't need percpu lock
  146. */
  147. if (state != TASK_RUNNING)
  148. prepare_to_wait(&pool->wait, &wait, state);
  149. if (!tags->nr_free)
  150. alloc_global_tags(pool, tags);
  151. if (!tags->nr_free)
  152. steal_tags(pool, tags);
  153. if (tags->nr_free) {
  154. tag = tags->freelist[--tags->nr_free];
  155. if (tags->nr_free)
  156. cpumask_set_cpu(smp_processor_id(),
  157. &pool->cpus_have_tags);
  158. }
  159. spin_unlock(&pool->lock);
  160. local_irq_restore(flags);
  161. if (tag >= 0 || state == TASK_RUNNING)
  162. break;
  163. if (signal_pending_state(state, current)) {
  164. tag = -ERESTARTSYS;
  165. break;
  166. }
  167. schedule();
  168. local_irq_save(flags);
  169. tags = this_cpu_ptr(pool->tag_cpu);
  170. }
  171. if (state != TASK_RUNNING)
  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);