sbitmap.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Fast and scalable bitmaps.
  3. *
  4. * Copyright (C) 2016 Facebook
  5. * Copyright (C) 2013-2014 Jens Axboe
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public
  9. * License v2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but 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. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __LINUX_SCALE_BITMAP_H
  20. #define __LINUX_SCALE_BITMAP_H
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. /**
  24. * struct sbitmap_word - Word in a &struct sbitmap.
  25. */
  26. struct sbitmap_word {
  27. /**
  28. * @word: The bitmap word itself.
  29. */
  30. unsigned long word;
  31. /**
  32. * @depth: Number of bits being used in @word.
  33. */
  34. unsigned long depth;
  35. } ____cacheline_aligned_in_smp;
  36. /**
  37. * struct sbitmap - Scalable bitmap.
  38. *
  39. * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This
  40. * trades off higher memory usage for better scalability.
  41. */
  42. struct sbitmap {
  43. /**
  44. * @depth: Number of bits used in the whole bitmap.
  45. */
  46. unsigned int depth;
  47. /**
  48. * @shift: log2(number of bits used per word)
  49. */
  50. unsigned int shift;
  51. /**
  52. * @map_nr: Number of words (cachelines) being used for the bitmap.
  53. */
  54. unsigned int map_nr;
  55. /**
  56. * @map: Allocated bitmap.
  57. */
  58. struct sbitmap_word *map;
  59. };
  60. #define SBQ_WAIT_QUEUES 8
  61. #define SBQ_WAKE_BATCH 8
  62. /**
  63. * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue.
  64. */
  65. struct sbq_wait_state {
  66. /**
  67. * @wait_cnt: Number of frees remaining before we wake up.
  68. */
  69. atomic_t wait_cnt;
  70. /**
  71. * @wait: Wait queue.
  72. */
  73. wait_queue_head_t wait;
  74. } ____cacheline_aligned_in_smp;
  75. /**
  76. * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free
  77. * bits.
  78. *
  79. * A &struct sbitmap_queue uses multiple wait queues and rolling wakeups to
  80. * avoid contention on the wait queue spinlock. This ensures that we don't hit a
  81. * scalability wall when we run out of free bits and have to start putting tasks
  82. * to sleep.
  83. */
  84. struct sbitmap_queue {
  85. /**
  86. * @sb: Scalable bitmap.
  87. */
  88. struct sbitmap sb;
  89. /*
  90. * @alloc_hint: Cache of last successfully allocated or freed bit.
  91. *
  92. * This is per-cpu, which allows multiple users to stick to different
  93. * cachelines until the map is exhausted.
  94. */
  95. unsigned int __percpu *alloc_hint;
  96. /**
  97. * @wake_batch: Number of bits which must be freed before we wake up any
  98. * waiters.
  99. */
  100. unsigned int wake_batch;
  101. /**
  102. * @wake_index: Next wait queue in @ws to wake up.
  103. */
  104. atomic_t wake_index;
  105. /**
  106. * @ws: Wait queues.
  107. */
  108. struct sbq_wait_state *ws;
  109. /**
  110. * @round_robin: Allocate bits in strict round-robin order.
  111. */
  112. bool round_robin;
  113. /**
  114. * @min_shallow_depth: The minimum shallow depth which may be passed to
  115. * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
  116. */
  117. unsigned int min_shallow_depth;
  118. };
  119. /**
  120. * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node.
  121. * @sb: Bitmap to initialize.
  122. * @depth: Number of bits to allocate.
  123. * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if
  124. * given, a good default is chosen.
  125. * @flags: Allocation flags.
  126. * @node: Memory node to allocate on.
  127. *
  128. * Return: Zero on success or negative errno on failure.
  129. */
  130. int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
  131. gfp_t flags, int node);
  132. /**
  133. * sbitmap_free() - Free memory used by a &struct sbitmap.
  134. * @sb: Bitmap to free.
  135. */
  136. static inline void sbitmap_free(struct sbitmap *sb)
  137. {
  138. kfree(sb->map);
  139. sb->map = NULL;
  140. }
  141. /**
  142. * sbitmap_resize() - Resize a &struct sbitmap.
  143. * @sb: Bitmap to resize.
  144. * @depth: New number of bits to resize to.
  145. *
  146. * Doesn't reallocate anything. It's up to the caller to ensure that the new
  147. * depth doesn't exceed the depth that the sb was initialized with.
  148. */
  149. void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
  150. /**
  151. * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
  152. * @sb: Bitmap to allocate from.
  153. * @alloc_hint: Hint for where to start searching for a free bit.
  154. * @round_robin: If true, be stricter about allocation order; always allocate
  155. * starting from the last allocated bit. This is less efficient
  156. * than the default behavior (false).
  157. *
  158. * This operation provides acquire barrier semantics if it succeeds.
  159. *
  160. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  161. */
  162. int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin);
  163. /**
  164. * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
  165. * limiting the depth used from each word.
  166. * @sb: Bitmap to allocate from.
  167. * @alloc_hint: Hint for where to start searching for a free bit.
  168. * @shallow_depth: The maximum number of bits to allocate from a single word.
  169. *
  170. * This rather specific operation allows for having multiple users with
  171. * different allocation limits. E.g., there can be a high-priority class that
  172. * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
  173. * with a @shallow_depth of (1 << (@sb->shift - 1)). Then, the low-priority
  174. * class can only allocate half of the total bits in the bitmap, preventing it
  175. * from starving out the high-priority class.
  176. *
  177. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  178. */
  179. int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
  180. unsigned long shallow_depth);
  181. /**
  182. * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap.
  183. * @sb: Bitmap to check.
  184. *
  185. * Return: true if any bit in the bitmap is set, false otherwise.
  186. */
  187. bool sbitmap_any_bit_set(const struct sbitmap *sb);
  188. /**
  189. * sbitmap_any_bit_clear() - Check for an unset bit in a &struct
  190. * sbitmap.
  191. * @sb: Bitmap to check.
  192. *
  193. * Return: true if any bit in the bitmap is clear, false otherwise.
  194. */
  195. bool sbitmap_any_bit_clear(const struct sbitmap *sb);
  196. #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
  197. #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
  198. typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
  199. /**
  200. * __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
  201. * @start: Where to start the iteration.
  202. * @sb: Bitmap to iterate over.
  203. * @fn: Callback. Should return true to continue or false to break early.
  204. * @data: Pointer to pass to callback.
  205. *
  206. * This is inline even though it's non-trivial so that the function calls to the
  207. * callback will hopefully get optimized away.
  208. */
  209. static inline void __sbitmap_for_each_set(struct sbitmap *sb,
  210. unsigned int start,
  211. sb_for_each_fn fn, void *data)
  212. {
  213. unsigned int index;
  214. unsigned int nr;
  215. unsigned int scanned = 0;
  216. if (start >= sb->depth)
  217. start = 0;
  218. index = SB_NR_TO_INDEX(sb, start);
  219. nr = SB_NR_TO_BIT(sb, start);
  220. while (scanned < sb->depth) {
  221. struct sbitmap_word *word = &sb->map[index];
  222. unsigned int depth = min_t(unsigned int, word->depth - nr,
  223. sb->depth - scanned);
  224. scanned += depth;
  225. if (!word->word)
  226. goto next;
  227. /*
  228. * On the first iteration of the outer loop, we need to add the
  229. * bit offset back to the size of the word for find_next_bit().
  230. * On all other iterations, nr is zero, so this is a noop.
  231. */
  232. depth += nr;
  233. while (1) {
  234. nr = find_next_bit(&word->word, depth, nr);
  235. if (nr >= depth)
  236. break;
  237. if (!fn(sb, (index << sb->shift) + nr, data))
  238. return;
  239. nr++;
  240. }
  241. next:
  242. nr = 0;
  243. if (++index >= sb->map_nr)
  244. index = 0;
  245. }
  246. }
  247. /**
  248. * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
  249. * @sb: Bitmap to iterate over.
  250. * @fn: Callback. Should return true to continue or false to break early.
  251. * @data: Pointer to pass to callback.
  252. */
  253. static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
  254. void *data)
  255. {
  256. __sbitmap_for_each_set(sb, 0, fn, data);
  257. }
  258. static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
  259. unsigned int bitnr)
  260. {
  261. return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word;
  262. }
  263. /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */
  264. static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr)
  265. {
  266. set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
  267. }
  268. static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
  269. {
  270. clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
  271. }
  272. static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb,
  273. unsigned int bitnr)
  274. {
  275. clear_bit_unlock(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
  276. }
  277. static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
  278. {
  279. return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
  280. }
  281. unsigned int sbitmap_weight(const struct sbitmap *sb);
  282. /**
  283. * sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file.
  284. * @sb: Bitmap to show.
  285. * @m: struct seq_file to write to.
  286. *
  287. * This is intended for debugging. The format may change at any time.
  288. */
  289. void sbitmap_show(struct sbitmap *sb, struct seq_file *m);
  290. /**
  291. * sbitmap_bitmap_show() - Write a hex dump of a &struct sbitmap to a &struct
  292. * seq_file.
  293. * @sb: Bitmap to show.
  294. * @m: struct seq_file to write to.
  295. *
  296. * This is intended for debugging. The output isn't guaranteed to be internally
  297. * consistent.
  298. */
  299. void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m);
  300. /**
  301. * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific
  302. * memory node.
  303. * @sbq: Bitmap queue to initialize.
  304. * @depth: See sbitmap_init_node().
  305. * @shift: See sbitmap_init_node().
  306. * @round_robin: See sbitmap_get().
  307. * @flags: Allocation flags.
  308. * @node: Memory node to allocate on.
  309. *
  310. * Return: Zero on success or negative errno on failure.
  311. */
  312. int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
  313. int shift, bool round_robin, gfp_t flags, int node);
  314. /**
  315. * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue.
  316. *
  317. * @sbq: Bitmap queue to free.
  318. */
  319. static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
  320. {
  321. kfree(sbq->ws);
  322. free_percpu(sbq->alloc_hint);
  323. sbitmap_free(&sbq->sb);
  324. }
  325. /**
  326. * sbitmap_queue_resize() - Resize a &struct sbitmap_queue.
  327. * @sbq: Bitmap queue to resize.
  328. * @depth: New number of bits to resize to.
  329. *
  330. * Like sbitmap_resize(), this doesn't reallocate anything. It has to do
  331. * some extra work on the &struct sbitmap_queue, so it's not safe to just
  332. * resize the underlying &struct sbitmap.
  333. */
  334. void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
  335. /**
  336. * __sbitmap_queue_get() - Try to allocate a free bit from a &struct
  337. * sbitmap_queue with preemption already disabled.
  338. * @sbq: Bitmap queue to allocate from.
  339. *
  340. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  341. */
  342. int __sbitmap_queue_get(struct sbitmap_queue *sbq);
  343. /**
  344. * __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
  345. * sbitmap_queue, limiting the depth used from each word, with preemption
  346. * already disabled.
  347. * @sbq: Bitmap queue to allocate from.
  348. * @shallow_depth: The maximum number of bits to allocate from a single word.
  349. * See sbitmap_get_shallow().
  350. *
  351. * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
  352. * initializing @sbq.
  353. *
  354. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  355. */
  356. int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  357. unsigned int shallow_depth);
  358. /**
  359. * sbitmap_queue_get() - Try to allocate a free bit from a &struct
  360. * sbitmap_queue.
  361. * @sbq: Bitmap queue to allocate from.
  362. * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
  363. * sbitmap_queue_clear()).
  364. *
  365. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  366. */
  367. static inline int sbitmap_queue_get(struct sbitmap_queue *sbq,
  368. unsigned int *cpu)
  369. {
  370. int nr;
  371. *cpu = get_cpu();
  372. nr = __sbitmap_queue_get(sbq);
  373. put_cpu();
  374. return nr;
  375. }
  376. /**
  377. * sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
  378. * sbitmap_queue, limiting the depth used from each word.
  379. * @sbq: Bitmap queue to allocate from.
  380. * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
  381. * sbitmap_queue_clear()).
  382. * @shallow_depth: The maximum number of bits to allocate from a single word.
  383. * See sbitmap_get_shallow().
  384. *
  385. * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
  386. * initializing @sbq.
  387. *
  388. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  389. */
  390. static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  391. unsigned int *cpu,
  392. unsigned int shallow_depth)
  393. {
  394. int nr;
  395. *cpu = get_cpu();
  396. nr = __sbitmap_queue_get_shallow(sbq, shallow_depth);
  397. put_cpu();
  398. return nr;
  399. }
  400. /**
  401. * sbitmap_queue_min_shallow_depth() - Inform a &struct sbitmap_queue of the
  402. * minimum shallow depth that will be used.
  403. * @sbq: Bitmap queue in question.
  404. * @min_shallow_depth: The minimum shallow depth that will be passed to
  405. * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
  406. *
  407. * sbitmap_queue_clear() batches wakeups as an optimization. The batch size
  408. * depends on the depth of the bitmap. Since the shallow allocation functions
  409. * effectively operate with a different depth, the shallow depth must be taken
  410. * into account when calculating the batch size. This function must be called
  411. * with the minimum shallow depth that will be used. Failure to do so can result
  412. * in missed wakeups.
  413. */
  414. void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
  415. unsigned int min_shallow_depth);
  416. /**
  417. * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
  418. * &struct sbitmap_queue.
  419. * @sbq: Bitmap to free from.
  420. * @nr: Bit number to free.
  421. * @cpu: CPU the bit was allocated on.
  422. */
  423. void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
  424. unsigned int cpu);
  425. static inline int sbq_index_inc(int index)
  426. {
  427. return (index + 1) & (SBQ_WAIT_QUEUES - 1);
  428. }
  429. static inline void sbq_index_atomic_inc(atomic_t *index)
  430. {
  431. int old = atomic_read(index);
  432. int new = sbq_index_inc(old);
  433. atomic_cmpxchg(index, old, new);
  434. }
  435. /**
  436. * sbq_wait_ptr() - Get the next wait queue to use for a &struct
  437. * sbitmap_queue.
  438. * @sbq: Bitmap queue to wait on.
  439. * @wait_index: A counter per "user" of @sbq.
  440. */
  441. static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq,
  442. atomic_t *wait_index)
  443. {
  444. struct sbq_wait_state *ws;
  445. ws = &sbq->ws[atomic_read(wait_index)];
  446. sbq_index_atomic_inc(wait_index);
  447. return ws;
  448. }
  449. /**
  450. * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct
  451. * sbitmap_queue.
  452. * @sbq: Bitmap queue to wake up.
  453. */
  454. void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
  455. /**
  456. * sbitmap_queue_wake_up() - Wake up some of waiters in one waitqueue
  457. * on a &struct sbitmap_queue.
  458. * @sbq: Bitmap queue to wake up.
  459. */
  460. void sbitmap_queue_wake_up(struct sbitmap_queue *sbq);
  461. /**
  462. * sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct
  463. * seq_file.
  464. * @sbq: Bitmap queue to show.
  465. * @m: struct seq_file to write to.
  466. *
  467. * This is intended for debugging. The format may change at any time.
  468. */
  469. void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m);
  470. #endif /* __LINUX_SCALE_BITMAP_H */