sbitmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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(unsigned long *word, unsigned long depth,
  72. unsigned int hint, bool wrap)
  73. {
  74. unsigned int orig_hint = hint;
  75. int nr;
  76. while (1) {
  77. nr = find_next_zero_bit(word, depth, hint);
  78. if (unlikely(nr >= 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))
  91. break;
  92. hint = nr + 1;
  93. if (hint >= 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].word,
  105. sb->map[index].depth,
  106. SB_NR_TO_BIT(sb, alloc_hint),
  107. !round_robin);
  108. if (nr != -1) {
  109. nr += index << sb->shift;
  110. break;
  111. }
  112. /* Jump to next index. */
  113. index++;
  114. alloc_hint = index << sb->shift;
  115. if (index >= sb->map_nr) {
  116. index = 0;
  117. alloc_hint = 0;
  118. }
  119. }
  120. return nr;
  121. }
  122. EXPORT_SYMBOL_GPL(sbitmap_get);
  123. int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
  124. unsigned long shallow_depth)
  125. {
  126. unsigned int i, index;
  127. int nr = -1;
  128. index = SB_NR_TO_INDEX(sb, alloc_hint);
  129. for (i = 0; i < sb->map_nr; i++) {
  130. nr = __sbitmap_get_word(&sb->map[index].word,
  131. min(sb->map[index].depth, shallow_depth),
  132. SB_NR_TO_BIT(sb, alloc_hint), true);
  133. if (nr != -1) {
  134. nr += index << sb->shift;
  135. break;
  136. }
  137. /* Jump to next index. */
  138. index++;
  139. alloc_hint = index << sb->shift;
  140. if (index >= sb->map_nr) {
  141. index = 0;
  142. alloc_hint = 0;
  143. }
  144. }
  145. return nr;
  146. }
  147. EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
  148. bool sbitmap_any_bit_set(const struct sbitmap *sb)
  149. {
  150. unsigned int i;
  151. for (i = 0; i < sb->map_nr; i++) {
  152. if (sb->map[i].word)
  153. return true;
  154. }
  155. return false;
  156. }
  157. EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
  158. bool sbitmap_any_bit_clear(const struct sbitmap *sb)
  159. {
  160. unsigned int i;
  161. for (i = 0; i < sb->map_nr; i++) {
  162. const struct sbitmap_word *word = &sb->map[i];
  163. unsigned long ret;
  164. ret = find_first_zero_bit(&word->word, word->depth);
  165. if (ret < word->depth)
  166. return true;
  167. }
  168. return false;
  169. }
  170. EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
  171. unsigned int sbitmap_weight(const struct sbitmap *sb)
  172. {
  173. unsigned int i, weight = 0;
  174. for (i = 0; i < sb->map_nr; i++) {
  175. const struct sbitmap_word *word = &sb->map[i];
  176. weight += bitmap_weight(&word->word, word->depth);
  177. }
  178. return weight;
  179. }
  180. EXPORT_SYMBOL_GPL(sbitmap_weight);
  181. void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
  182. {
  183. seq_printf(m, "depth=%u\n", sb->depth);
  184. seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
  185. seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
  186. seq_printf(m, "map_nr=%u\n", sb->map_nr);
  187. }
  188. EXPORT_SYMBOL_GPL(sbitmap_show);
  189. static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
  190. {
  191. if ((offset & 0xf) == 0) {
  192. if (offset != 0)
  193. seq_putc(m, '\n');
  194. seq_printf(m, "%08x:", offset);
  195. }
  196. if ((offset & 0x1) == 0)
  197. seq_putc(m, ' ');
  198. seq_printf(m, "%02x", byte);
  199. }
  200. void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
  201. {
  202. u8 byte = 0;
  203. unsigned int byte_bits = 0;
  204. unsigned int offset = 0;
  205. int i;
  206. for (i = 0; i < sb->map_nr; i++) {
  207. unsigned long word = READ_ONCE(sb->map[i].word);
  208. unsigned int word_bits = READ_ONCE(sb->map[i].depth);
  209. while (word_bits > 0) {
  210. unsigned int bits = min(8 - byte_bits, word_bits);
  211. byte |= (word & (BIT(bits) - 1)) << byte_bits;
  212. byte_bits += bits;
  213. if (byte_bits == 8) {
  214. emit_byte(m, offset, byte);
  215. byte = 0;
  216. byte_bits = 0;
  217. offset++;
  218. }
  219. word >>= bits;
  220. word_bits -= bits;
  221. }
  222. }
  223. if (byte_bits) {
  224. emit_byte(m, offset, byte);
  225. offset++;
  226. }
  227. if (offset)
  228. seq_putc(m, '\n');
  229. }
  230. EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
  231. static unsigned int sbq_calc_wake_batch(unsigned int depth)
  232. {
  233. unsigned int wake_batch;
  234. /*
  235. * For each batch, we wake up one queue. We need to make sure that our
  236. * batch size is small enough that the full depth of the bitmap is
  237. * enough to wake up all of the queues.
  238. */
  239. wake_batch = SBQ_WAKE_BATCH;
  240. if (wake_batch > depth / SBQ_WAIT_QUEUES)
  241. wake_batch = max(1U, depth / SBQ_WAIT_QUEUES);
  242. return wake_batch;
  243. }
  244. int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
  245. int shift, bool round_robin, gfp_t flags, int node)
  246. {
  247. int ret;
  248. int i;
  249. ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
  250. if (ret)
  251. return ret;
  252. sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
  253. if (!sbq->alloc_hint) {
  254. sbitmap_free(&sbq->sb);
  255. return -ENOMEM;
  256. }
  257. if (depth && !round_robin) {
  258. for_each_possible_cpu(i)
  259. *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
  260. }
  261. sbq->wake_batch = sbq_calc_wake_batch(depth);
  262. atomic_set(&sbq->wake_index, 0);
  263. sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
  264. if (!sbq->ws) {
  265. free_percpu(sbq->alloc_hint);
  266. sbitmap_free(&sbq->sb);
  267. return -ENOMEM;
  268. }
  269. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  270. init_waitqueue_head(&sbq->ws[i].wait);
  271. atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
  272. }
  273. sbq->round_robin = round_robin;
  274. return 0;
  275. }
  276. EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
  277. void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
  278. {
  279. unsigned int wake_batch = sbq_calc_wake_batch(depth);
  280. int i;
  281. if (sbq->wake_batch != wake_batch) {
  282. WRITE_ONCE(sbq->wake_batch, wake_batch);
  283. /*
  284. * Pairs with the memory barrier in sbq_wake_up() to ensure that
  285. * the batch size is updated before the wait counts.
  286. */
  287. smp_mb__before_atomic();
  288. for (i = 0; i < SBQ_WAIT_QUEUES; i++)
  289. atomic_set(&sbq->ws[i].wait_cnt, 1);
  290. }
  291. sbitmap_resize(&sbq->sb, depth);
  292. }
  293. EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
  294. int __sbitmap_queue_get(struct sbitmap_queue *sbq)
  295. {
  296. unsigned int hint, depth;
  297. int nr;
  298. hint = this_cpu_read(*sbq->alloc_hint);
  299. depth = READ_ONCE(sbq->sb.depth);
  300. if (unlikely(hint >= depth)) {
  301. hint = depth ? prandom_u32() % depth : 0;
  302. this_cpu_write(*sbq->alloc_hint, hint);
  303. }
  304. nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
  305. if (nr == -1) {
  306. /* If the map is full, a hint won't do us much good. */
  307. this_cpu_write(*sbq->alloc_hint, 0);
  308. } else if (nr == hint || unlikely(sbq->round_robin)) {
  309. /* Only update the hint if we used it. */
  310. hint = nr + 1;
  311. if (hint >= depth - 1)
  312. hint = 0;
  313. this_cpu_write(*sbq->alloc_hint, hint);
  314. }
  315. return nr;
  316. }
  317. EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
  318. int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  319. unsigned int shallow_depth)
  320. {
  321. unsigned int hint, depth;
  322. int nr;
  323. hint = this_cpu_read(*sbq->alloc_hint);
  324. depth = READ_ONCE(sbq->sb.depth);
  325. if (unlikely(hint >= depth)) {
  326. hint = depth ? prandom_u32() % depth : 0;
  327. this_cpu_write(*sbq->alloc_hint, hint);
  328. }
  329. nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
  330. if (nr == -1) {
  331. /* If the map is full, a hint won't do us much good. */
  332. this_cpu_write(*sbq->alloc_hint, 0);
  333. } else if (nr == hint || unlikely(sbq->round_robin)) {
  334. /* Only update the hint if we used it. */
  335. hint = nr + 1;
  336. if (hint >= depth - 1)
  337. hint = 0;
  338. this_cpu_write(*sbq->alloc_hint, hint);
  339. }
  340. return nr;
  341. }
  342. EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
  343. static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
  344. {
  345. int i, wake_index;
  346. wake_index = atomic_read(&sbq->wake_index);
  347. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  348. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  349. if (waitqueue_active(&ws->wait)) {
  350. int o = atomic_read(&sbq->wake_index);
  351. if (wake_index != o)
  352. atomic_cmpxchg(&sbq->wake_index, o, wake_index);
  353. return ws;
  354. }
  355. wake_index = sbq_index_inc(wake_index);
  356. }
  357. return NULL;
  358. }
  359. static void sbq_wake_up(struct sbitmap_queue *sbq)
  360. {
  361. struct sbq_wait_state *ws;
  362. unsigned int wake_batch;
  363. int wait_cnt;
  364. /*
  365. * Pairs with the memory barrier in set_current_state() to ensure the
  366. * proper ordering of clear_bit()/waitqueue_active() in the waker and
  367. * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See
  368. * the comment on waitqueue_active(). This is __after_atomic because we
  369. * just did clear_bit() in the caller.
  370. */
  371. smp_mb__after_atomic();
  372. ws = sbq_wake_ptr(sbq);
  373. if (!ws)
  374. return;
  375. wait_cnt = atomic_dec_return(&ws->wait_cnt);
  376. if (wait_cnt <= 0) {
  377. wake_batch = READ_ONCE(sbq->wake_batch);
  378. /*
  379. * Pairs with the memory barrier in sbitmap_queue_resize() to
  380. * ensure that we see the batch size update before the wait
  381. * count is reset.
  382. */
  383. smp_mb__before_atomic();
  384. /*
  385. * If there are concurrent callers to sbq_wake_up(), the last
  386. * one to decrement the wait count below zero will bump it back
  387. * up. If there is a concurrent resize, the count reset will
  388. * either cause the cmpxchg to fail or overwrite after the
  389. * cmpxchg.
  390. */
  391. atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wait_cnt + wake_batch);
  392. sbq_index_atomic_inc(&sbq->wake_index);
  393. wake_up_nr(&ws->wait, wake_batch);
  394. }
  395. }
  396. void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
  397. unsigned int cpu)
  398. {
  399. sbitmap_clear_bit(&sbq->sb, nr);
  400. sbq_wake_up(sbq);
  401. if (likely(!sbq->round_robin && nr < sbq->sb.depth))
  402. *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
  403. }
  404. EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
  405. void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
  406. {
  407. int i, wake_index;
  408. /*
  409. * Pairs with the memory barrier in set_current_state() like in
  410. * sbq_wake_up().
  411. */
  412. smp_mb();
  413. wake_index = atomic_read(&sbq->wake_index);
  414. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  415. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  416. if (waitqueue_active(&ws->wait))
  417. wake_up(&ws->wait);
  418. wake_index = sbq_index_inc(wake_index);
  419. }
  420. }
  421. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
  422. void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
  423. {
  424. bool first;
  425. int i;
  426. sbitmap_show(&sbq->sb, m);
  427. seq_puts(m, "alloc_hint={");
  428. first = true;
  429. for_each_possible_cpu(i) {
  430. if (!first)
  431. seq_puts(m, ", ");
  432. first = false;
  433. seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
  434. }
  435. seq_puts(m, "}\n");
  436. seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
  437. seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
  438. seq_puts(m, "ws={\n");
  439. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  440. struct sbq_wait_state *ws = &sbq->ws[i];
  441. seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
  442. atomic_read(&ws->wait_cnt),
  443. waitqueue_active(&ws->wait) ? "active" : "inactive");
  444. }
  445. seq_puts(m, "}\n");
  446. seq_printf(m, "round_robin=%d\n", sbq->round_robin);
  447. }
  448. EXPORT_SYMBOL_GPL(sbitmap_queue_show);