sbitmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (C) 2016 Facebook
  3. * Copyright (C) 2013-2014 Jens Axboe
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/random.h>
  19. #include <linux/sbitmap.h>
  20. #include <linux/seq_file.h>
  21. int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
  22. gfp_t flags, int node)
  23. {
  24. unsigned int bits_per_word;
  25. unsigned int i;
  26. if (shift < 0) {
  27. shift = ilog2(BITS_PER_LONG);
  28. /*
  29. * If the bitmap is small, shrink the number of bits per word so
  30. * we spread over a few cachelines, at least. If less than 4
  31. * bits, just forget about it, it's not going to work optimally
  32. * anyway.
  33. */
  34. if (depth >= 4) {
  35. while ((4U << shift) > depth)
  36. shift--;
  37. }
  38. }
  39. bits_per_word = 1U << shift;
  40. if (bits_per_word > BITS_PER_LONG)
  41. return -EINVAL;
  42. sb->shift = shift;
  43. sb->depth = depth;
  44. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  45. if (depth == 0) {
  46. sb->map = NULL;
  47. return 0;
  48. }
  49. sb->map = kzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
  50. if (!sb->map)
  51. return -ENOMEM;
  52. for (i = 0; i < sb->map_nr; i++) {
  53. sb->map[i].depth = min(depth, bits_per_word);
  54. depth -= sb->map[i].depth;
  55. }
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(sbitmap_init_node);
  59. void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
  60. {
  61. unsigned int bits_per_word = 1U << sb->shift;
  62. unsigned int i;
  63. sb->depth = depth;
  64. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  65. for (i = 0; i < sb->map_nr; i++) {
  66. sb->map[i].depth = min(depth, bits_per_word);
  67. depth -= sb->map[i].depth;
  68. }
  69. }
  70. EXPORT_SYMBOL_GPL(sbitmap_resize);
  71. static int __sbitmap_get_word(struct sbitmap_word *word, unsigned int hint,
  72. bool wrap)
  73. {
  74. unsigned int orig_hint = hint;
  75. int nr;
  76. while (1) {
  77. nr = find_next_zero_bit(&word->word, word->depth, hint);
  78. if (unlikely(nr >= word->depth)) {
  79. /*
  80. * We started with an offset, and we didn't reset the
  81. * offset to 0 in a failure case, so start from 0 to
  82. * exhaust the map.
  83. */
  84. if (orig_hint && hint && wrap) {
  85. hint = orig_hint = 0;
  86. continue;
  87. }
  88. return -1;
  89. }
  90. if (!test_and_set_bit(nr, &word->word))
  91. break;
  92. hint = nr + 1;
  93. if (hint >= word->depth - 1)
  94. hint = 0;
  95. }
  96. return nr;
  97. }
  98. int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
  99. {
  100. unsigned int i, index;
  101. int nr = -1;
  102. index = SB_NR_TO_INDEX(sb, alloc_hint);
  103. for (i = 0; i < sb->map_nr; i++) {
  104. nr = __sbitmap_get_word(&sb->map[index],
  105. SB_NR_TO_BIT(sb, alloc_hint),
  106. !round_robin);
  107. if (nr != -1) {
  108. nr += index << sb->shift;
  109. break;
  110. }
  111. /* Jump to next index. */
  112. index++;
  113. alloc_hint = index << sb->shift;
  114. if (index >= sb->map_nr) {
  115. index = 0;
  116. alloc_hint = 0;
  117. }
  118. }
  119. return nr;
  120. }
  121. EXPORT_SYMBOL_GPL(sbitmap_get);
  122. bool sbitmap_any_bit_set(const struct sbitmap *sb)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < sb->map_nr; i++) {
  126. if (sb->map[i].word)
  127. return true;
  128. }
  129. return false;
  130. }
  131. EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
  132. bool sbitmap_any_bit_clear(const struct sbitmap *sb)
  133. {
  134. unsigned int i;
  135. for (i = 0; i < sb->map_nr; i++) {
  136. const struct sbitmap_word *word = &sb->map[i];
  137. unsigned long ret;
  138. ret = find_first_zero_bit(&word->word, word->depth);
  139. if (ret < word->depth)
  140. return true;
  141. }
  142. return false;
  143. }
  144. EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
  145. unsigned int sbitmap_weight(const struct sbitmap *sb)
  146. {
  147. unsigned int i, weight = 0;
  148. for (i = 0; i < sb->map_nr; i++) {
  149. const struct sbitmap_word *word = &sb->map[i];
  150. weight += bitmap_weight(&word->word, word->depth);
  151. }
  152. return weight;
  153. }
  154. EXPORT_SYMBOL_GPL(sbitmap_weight);
  155. void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
  156. {
  157. seq_printf(m, "depth=%u\n", sb->depth);
  158. seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
  159. seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
  160. seq_printf(m, "map_nr=%u\n", sb->map_nr);
  161. }
  162. EXPORT_SYMBOL_GPL(sbitmap_show);
  163. static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
  164. {
  165. if ((offset & 0xf) == 0) {
  166. if (offset != 0)
  167. seq_putc(m, '\n');
  168. seq_printf(m, "%08x:", offset);
  169. }
  170. if ((offset & 0x1) == 0)
  171. seq_putc(m, ' ');
  172. seq_printf(m, "%02x", byte);
  173. }
  174. void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
  175. {
  176. u8 byte = 0;
  177. unsigned int byte_bits = 0;
  178. unsigned int offset = 0;
  179. int i;
  180. for (i = 0; i < sb->map_nr; i++) {
  181. unsigned long word = READ_ONCE(sb->map[i].word);
  182. unsigned int word_bits = READ_ONCE(sb->map[i].depth);
  183. while (word_bits > 0) {
  184. unsigned int bits = min(8 - byte_bits, word_bits);
  185. byte |= (word & (BIT(bits) - 1)) << byte_bits;
  186. byte_bits += bits;
  187. if (byte_bits == 8) {
  188. emit_byte(m, offset, byte);
  189. byte = 0;
  190. byte_bits = 0;
  191. offset++;
  192. }
  193. word >>= bits;
  194. word_bits -= bits;
  195. }
  196. }
  197. if (byte_bits) {
  198. emit_byte(m, offset, byte);
  199. offset++;
  200. }
  201. if (offset)
  202. seq_putc(m, '\n');
  203. }
  204. EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
  205. static unsigned int sbq_calc_wake_batch(unsigned int depth)
  206. {
  207. unsigned int wake_batch;
  208. /*
  209. * For each batch, we wake up one queue. We need to make sure that our
  210. * batch size is small enough that the full depth of the bitmap is
  211. * enough to wake up all of the queues.
  212. */
  213. wake_batch = SBQ_WAKE_BATCH;
  214. if (wake_batch > depth / SBQ_WAIT_QUEUES)
  215. wake_batch = max(1U, depth / SBQ_WAIT_QUEUES);
  216. return wake_batch;
  217. }
  218. int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
  219. int shift, bool round_robin, gfp_t flags, int node)
  220. {
  221. int ret;
  222. int i;
  223. ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
  224. if (ret)
  225. return ret;
  226. sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
  227. if (!sbq->alloc_hint) {
  228. sbitmap_free(&sbq->sb);
  229. return -ENOMEM;
  230. }
  231. if (depth && !round_robin) {
  232. for_each_possible_cpu(i)
  233. *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
  234. }
  235. sbq->wake_batch = sbq_calc_wake_batch(depth);
  236. atomic_set(&sbq->wake_index, 0);
  237. sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
  238. if (!sbq->ws) {
  239. free_percpu(sbq->alloc_hint);
  240. sbitmap_free(&sbq->sb);
  241. return -ENOMEM;
  242. }
  243. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  244. init_waitqueue_head(&sbq->ws[i].wait);
  245. atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
  246. }
  247. sbq->round_robin = round_robin;
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
  251. void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
  252. {
  253. unsigned int wake_batch = sbq_calc_wake_batch(depth);
  254. int i;
  255. if (sbq->wake_batch != wake_batch) {
  256. WRITE_ONCE(sbq->wake_batch, wake_batch);
  257. /*
  258. * Pairs with the memory barrier in sbq_wake_up() to ensure that
  259. * the batch size is updated before the wait counts.
  260. */
  261. smp_mb__before_atomic();
  262. for (i = 0; i < SBQ_WAIT_QUEUES; i++)
  263. atomic_set(&sbq->ws[i].wait_cnt, 1);
  264. }
  265. sbitmap_resize(&sbq->sb, depth);
  266. }
  267. EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
  268. int __sbitmap_queue_get(struct sbitmap_queue *sbq)
  269. {
  270. unsigned int hint, depth;
  271. int nr;
  272. hint = this_cpu_read(*sbq->alloc_hint);
  273. depth = READ_ONCE(sbq->sb.depth);
  274. if (unlikely(hint >= depth)) {
  275. hint = depth ? prandom_u32() % depth : 0;
  276. this_cpu_write(*sbq->alloc_hint, hint);
  277. }
  278. nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
  279. if (nr == -1) {
  280. /* If the map is full, a hint won't do us much good. */
  281. this_cpu_write(*sbq->alloc_hint, 0);
  282. } else if (nr == hint || unlikely(sbq->round_robin)) {
  283. /* Only update the hint if we used it. */
  284. hint = nr + 1;
  285. if (hint >= depth - 1)
  286. hint = 0;
  287. this_cpu_write(*sbq->alloc_hint, hint);
  288. }
  289. return nr;
  290. }
  291. EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
  292. static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
  293. {
  294. int i, wake_index;
  295. wake_index = atomic_read(&sbq->wake_index);
  296. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  297. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  298. if (waitqueue_active(&ws->wait)) {
  299. int o = atomic_read(&sbq->wake_index);
  300. if (wake_index != o)
  301. atomic_cmpxchg(&sbq->wake_index, o, wake_index);
  302. return ws;
  303. }
  304. wake_index = sbq_index_inc(wake_index);
  305. }
  306. return NULL;
  307. }
  308. static void sbq_wake_up(struct sbitmap_queue *sbq)
  309. {
  310. struct sbq_wait_state *ws;
  311. unsigned int wake_batch;
  312. int wait_cnt;
  313. /*
  314. * Pairs with the memory barrier in set_current_state() to ensure the
  315. * proper ordering of clear_bit()/waitqueue_active() in the waker and
  316. * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See
  317. * the comment on waitqueue_active(). This is __after_atomic because we
  318. * just did clear_bit() in the caller.
  319. */
  320. smp_mb__after_atomic();
  321. ws = sbq_wake_ptr(sbq);
  322. if (!ws)
  323. return;
  324. wait_cnt = atomic_dec_return(&ws->wait_cnt);
  325. if (wait_cnt <= 0) {
  326. wake_batch = READ_ONCE(sbq->wake_batch);
  327. /*
  328. * Pairs with the memory barrier in sbitmap_queue_resize() to
  329. * ensure that we see the batch size update before the wait
  330. * count is reset.
  331. */
  332. smp_mb__before_atomic();
  333. /*
  334. * If there are concurrent callers to sbq_wake_up(), the last
  335. * one to decrement the wait count below zero will bump it back
  336. * up. If there is a concurrent resize, the count reset will
  337. * either cause the cmpxchg to fail or overwrite after the
  338. * cmpxchg.
  339. */
  340. atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wait_cnt + wake_batch);
  341. sbq_index_atomic_inc(&sbq->wake_index);
  342. wake_up(&ws->wait);
  343. }
  344. }
  345. void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
  346. unsigned int cpu)
  347. {
  348. sbitmap_clear_bit(&sbq->sb, nr);
  349. sbq_wake_up(sbq);
  350. if (likely(!sbq->round_robin && nr < sbq->sb.depth))
  351. *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
  352. }
  353. EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
  354. void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
  355. {
  356. int i, wake_index;
  357. /*
  358. * Pairs with the memory barrier in set_current_state() like in
  359. * sbq_wake_up().
  360. */
  361. smp_mb();
  362. wake_index = atomic_read(&sbq->wake_index);
  363. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  364. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  365. if (waitqueue_active(&ws->wait))
  366. wake_up(&ws->wait);
  367. wake_index = sbq_index_inc(wake_index);
  368. }
  369. }
  370. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
  371. void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
  372. {
  373. bool first;
  374. int i;
  375. sbitmap_show(&sbq->sb, m);
  376. seq_puts(m, "alloc_hint={");
  377. first = true;
  378. for_each_possible_cpu(i) {
  379. if (!first)
  380. seq_puts(m, ", ");
  381. first = false;
  382. seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
  383. }
  384. seq_puts(m, "}\n");
  385. seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
  386. seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
  387. seq_puts(m, "ws={\n");
  388. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  389. struct sbq_wait_state *ws = &sbq->ws[i];
  390. seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
  391. atomic_read(&ws->wait_cnt),
  392. waitqueue_active(&ws->wait) ? "active" : "inactive");
  393. }
  394. seq_puts(m, "}\n");
  395. seq_printf(m, "round_robin=%d\n", sbq->round_robin);
  396. }
  397. EXPORT_SYMBOL_GPL(sbitmap_queue_show);