sbitmap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Facebook
  4. * Copyright (C) 2013-2014 Jens Axboe
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/random.h>
  8. #include <linux/sbitmap.h>
  9. #include <linux/seq_file.h>
  10. static int init_alloc_hint(struct sbitmap *sb, gfp_t flags)
  11. {
  12. unsigned depth = sb->depth;
  13. sb->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
  14. if (!sb->alloc_hint)
  15. return -ENOMEM;
  16. if (depth && !sb->round_robin) {
  17. int i;
  18. for_each_possible_cpu(i)
  19. *per_cpu_ptr(sb->alloc_hint, i) = prandom_u32_max(depth);
  20. }
  21. return 0;
  22. }
  23. static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb,
  24. unsigned int depth)
  25. {
  26. unsigned hint;
  27. hint = this_cpu_read(*sb->alloc_hint);
  28. if (unlikely(hint >= depth)) {
  29. hint = depth ? prandom_u32_max(depth) : 0;
  30. this_cpu_write(*sb->alloc_hint, hint);
  31. }
  32. return hint;
  33. }
  34. static inline void update_alloc_hint_after_get(struct sbitmap *sb,
  35. unsigned int depth,
  36. unsigned int hint,
  37. unsigned int nr)
  38. {
  39. if (nr == -1) {
  40. /* If the map is full, a hint won't do us much good. */
  41. this_cpu_write(*sb->alloc_hint, 0);
  42. } else if (nr == hint || unlikely(sb->round_robin)) {
  43. /* Only update the hint if we used it. */
  44. hint = nr + 1;
  45. if (hint >= depth - 1)
  46. hint = 0;
  47. this_cpu_write(*sb->alloc_hint, hint);
  48. }
  49. }
  50. /*
  51. * See if we have deferred clears that we can batch move
  52. */
  53. static inline bool sbitmap_deferred_clear(struct sbitmap_word *map)
  54. {
  55. unsigned long mask;
  56. if (!READ_ONCE(map->cleared))
  57. return false;
  58. /*
  59. * First get a stable cleared mask, setting the old mask to 0.
  60. */
  61. mask = xchg(&map->cleared, 0);
  62. /*
  63. * Now clear the masked bits in our free word
  64. */
  65. atomic_long_andnot(mask, (atomic_long_t *)&map->word);
  66. BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word));
  67. return true;
  68. }
  69. int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
  70. gfp_t flags, int node, bool round_robin,
  71. bool alloc_hint)
  72. {
  73. unsigned int bits_per_word;
  74. if (shift < 0)
  75. shift = sbitmap_calculate_shift(depth);
  76. bits_per_word = 1U << shift;
  77. if (bits_per_word > BITS_PER_LONG)
  78. return -EINVAL;
  79. sb->shift = shift;
  80. sb->depth = depth;
  81. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  82. sb->round_robin = round_robin;
  83. if (depth == 0) {
  84. sb->map = NULL;
  85. return 0;
  86. }
  87. if (alloc_hint) {
  88. if (init_alloc_hint(sb, flags))
  89. return -ENOMEM;
  90. } else {
  91. sb->alloc_hint = NULL;
  92. }
  93. sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
  94. if (!sb->map) {
  95. free_percpu(sb->alloc_hint);
  96. return -ENOMEM;
  97. }
  98. return 0;
  99. }
  100. EXPORT_SYMBOL_GPL(sbitmap_init_node);
  101. void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
  102. {
  103. unsigned int bits_per_word = 1U << sb->shift;
  104. unsigned int i;
  105. for (i = 0; i < sb->map_nr; i++)
  106. sbitmap_deferred_clear(&sb->map[i]);
  107. sb->depth = depth;
  108. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  109. }
  110. EXPORT_SYMBOL_GPL(sbitmap_resize);
  111. static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
  112. unsigned int hint, bool wrap)
  113. {
  114. int nr;
  115. /* don't wrap if starting from 0 */
  116. wrap = wrap && hint;
  117. while (1) {
  118. nr = find_next_zero_bit(word, depth, hint);
  119. if (unlikely(nr >= depth)) {
  120. /*
  121. * We started with an offset, and we didn't reset the
  122. * offset to 0 in a failure case, so start from 0 to
  123. * exhaust the map.
  124. */
  125. if (hint && wrap) {
  126. hint = 0;
  127. continue;
  128. }
  129. return -1;
  130. }
  131. if (!test_and_set_bit_lock(nr, word))
  132. break;
  133. hint = nr + 1;
  134. if (hint >= depth - 1)
  135. hint = 0;
  136. }
  137. return nr;
  138. }
  139. static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
  140. unsigned int alloc_hint)
  141. {
  142. struct sbitmap_word *map = &sb->map[index];
  143. int nr;
  144. do {
  145. nr = __sbitmap_get_word(&map->word, __map_depth(sb, index),
  146. alloc_hint, !sb->round_robin);
  147. if (nr != -1)
  148. break;
  149. if (!sbitmap_deferred_clear(map))
  150. break;
  151. } while (1);
  152. return nr;
  153. }
  154. static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
  155. {
  156. unsigned int i, index;
  157. int nr = -1;
  158. index = SB_NR_TO_INDEX(sb, alloc_hint);
  159. /*
  160. * Unless we're doing round robin tag allocation, just use the
  161. * alloc_hint to find the right word index. No point in looping
  162. * twice in find_next_zero_bit() for that case.
  163. */
  164. if (sb->round_robin)
  165. alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
  166. else
  167. alloc_hint = 0;
  168. for (i = 0; i < sb->map_nr; i++) {
  169. nr = sbitmap_find_bit_in_index(sb, index, alloc_hint);
  170. if (nr != -1) {
  171. nr += index << sb->shift;
  172. break;
  173. }
  174. /* Jump to next index. */
  175. alloc_hint = 0;
  176. if (++index >= sb->map_nr)
  177. index = 0;
  178. }
  179. return nr;
  180. }
  181. int sbitmap_get(struct sbitmap *sb)
  182. {
  183. int nr;
  184. unsigned int hint, depth;
  185. if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
  186. return -1;
  187. depth = READ_ONCE(sb->depth);
  188. hint = update_alloc_hint_before_get(sb, depth);
  189. nr = __sbitmap_get(sb, hint);
  190. update_alloc_hint_after_get(sb, depth, hint, nr);
  191. return nr;
  192. }
  193. EXPORT_SYMBOL_GPL(sbitmap_get);
  194. static int __sbitmap_get_shallow(struct sbitmap *sb,
  195. unsigned int alloc_hint,
  196. unsigned long shallow_depth)
  197. {
  198. unsigned int i, index;
  199. int nr = -1;
  200. index = SB_NR_TO_INDEX(sb, alloc_hint);
  201. for (i = 0; i < sb->map_nr; i++) {
  202. again:
  203. nr = __sbitmap_get_word(&sb->map[index].word,
  204. min_t(unsigned int,
  205. __map_depth(sb, index),
  206. shallow_depth),
  207. SB_NR_TO_BIT(sb, alloc_hint), true);
  208. if (nr != -1) {
  209. nr += index << sb->shift;
  210. break;
  211. }
  212. if (sbitmap_deferred_clear(&sb->map[index]))
  213. goto again;
  214. /* Jump to next index. */
  215. index++;
  216. alloc_hint = index << sb->shift;
  217. if (index >= sb->map_nr) {
  218. index = 0;
  219. alloc_hint = 0;
  220. }
  221. }
  222. return nr;
  223. }
  224. int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)
  225. {
  226. int nr;
  227. unsigned int hint, depth;
  228. if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
  229. return -1;
  230. depth = READ_ONCE(sb->depth);
  231. hint = update_alloc_hint_before_get(sb, depth);
  232. nr = __sbitmap_get_shallow(sb, hint, shallow_depth);
  233. update_alloc_hint_after_get(sb, depth, hint, nr);
  234. return nr;
  235. }
  236. EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
  237. bool sbitmap_any_bit_set(const struct sbitmap *sb)
  238. {
  239. unsigned int i;
  240. for (i = 0; i < sb->map_nr; i++) {
  241. if (sb->map[i].word & ~sb->map[i].cleared)
  242. return true;
  243. }
  244. return false;
  245. }
  246. EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
  247. static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
  248. {
  249. unsigned int i, weight = 0;
  250. for (i = 0; i < sb->map_nr; i++) {
  251. const struct sbitmap_word *word = &sb->map[i];
  252. unsigned int word_depth = __map_depth(sb, i);
  253. if (set)
  254. weight += bitmap_weight(&word->word, word_depth);
  255. else
  256. weight += bitmap_weight(&word->cleared, word_depth);
  257. }
  258. return weight;
  259. }
  260. static unsigned int sbitmap_cleared(const struct sbitmap *sb)
  261. {
  262. return __sbitmap_weight(sb, false);
  263. }
  264. unsigned int sbitmap_weight(const struct sbitmap *sb)
  265. {
  266. return __sbitmap_weight(sb, true) - sbitmap_cleared(sb);
  267. }
  268. EXPORT_SYMBOL_GPL(sbitmap_weight);
  269. void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
  270. {
  271. seq_printf(m, "depth=%u\n", sb->depth);
  272. seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
  273. seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
  274. seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
  275. seq_printf(m, "map_nr=%u\n", sb->map_nr);
  276. }
  277. EXPORT_SYMBOL_GPL(sbitmap_show);
  278. static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
  279. {
  280. if ((offset & 0xf) == 0) {
  281. if (offset != 0)
  282. seq_putc(m, '\n');
  283. seq_printf(m, "%08x:", offset);
  284. }
  285. if ((offset & 0x1) == 0)
  286. seq_putc(m, ' ');
  287. seq_printf(m, "%02x", byte);
  288. }
  289. void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
  290. {
  291. u8 byte = 0;
  292. unsigned int byte_bits = 0;
  293. unsigned int offset = 0;
  294. int i;
  295. for (i = 0; i < sb->map_nr; i++) {
  296. unsigned long word = READ_ONCE(sb->map[i].word);
  297. unsigned long cleared = READ_ONCE(sb->map[i].cleared);
  298. unsigned int word_bits = __map_depth(sb, i);
  299. word &= ~cleared;
  300. while (word_bits > 0) {
  301. unsigned int bits = min(8 - byte_bits, word_bits);
  302. byte |= (word & (BIT(bits) - 1)) << byte_bits;
  303. byte_bits += bits;
  304. if (byte_bits == 8) {
  305. emit_byte(m, offset, byte);
  306. byte = 0;
  307. byte_bits = 0;
  308. offset++;
  309. }
  310. word >>= bits;
  311. word_bits -= bits;
  312. }
  313. }
  314. if (byte_bits) {
  315. emit_byte(m, offset, byte);
  316. offset++;
  317. }
  318. if (offset)
  319. seq_putc(m, '\n');
  320. }
  321. EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
  322. static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
  323. unsigned int depth)
  324. {
  325. unsigned int wake_batch;
  326. unsigned int shallow_depth;
  327. /*
  328. * For each batch, we wake up one queue. We need to make sure that our
  329. * batch size is small enough that the full depth of the bitmap,
  330. * potentially limited by a shallow depth, is enough to wake up all of
  331. * the queues.
  332. *
  333. * Each full word of the bitmap has bits_per_word bits, and there might
  334. * be a partial word. There are depth / bits_per_word full words and
  335. * depth % bits_per_word bits left over. In bitwise arithmetic:
  336. *
  337. * bits_per_word = 1 << shift
  338. * depth / bits_per_word = depth >> shift
  339. * depth % bits_per_word = depth & ((1 << shift) - 1)
  340. *
  341. * Each word can be limited to sbq->min_shallow_depth bits.
  342. */
  343. shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
  344. depth = ((depth >> sbq->sb.shift) * shallow_depth +
  345. min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
  346. wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
  347. SBQ_WAKE_BATCH);
  348. return wake_batch;
  349. }
  350. int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
  351. int shift, bool round_robin, gfp_t flags, int node)
  352. {
  353. int ret;
  354. int i;
  355. ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,
  356. round_robin, true);
  357. if (ret)
  358. return ret;
  359. sbq->min_shallow_depth = UINT_MAX;
  360. sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
  361. atomic_set(&sbq->wake_index, 0);
  362. atomic_set(&sbq->ws_active, 0);
  363. atomic_set(&sbq->completion_cnt, 0);
  364. atomic_set(&sbq->wakeup_cnt, 0);
  365. sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
  366. if (!sbq->ws) {
  367. sbitmap_free(&sbq->sb);
  368. return -ENOMEM;
  369. }
  370. for (i = 0; i < SBQ_WAIT_QUEUES; i++)
  371. init_waitqueue_head(&sbq->ws[i].wait);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
  375. static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
  376. unsigned int depth)
  377. {
  378. unsigned int wake_batch;
  379. wake_batch = sbq_calc_wake_batch(sbq, depth);
  380. if (sbq->wake_batch != wake_batch)
  381. WRITE_ONCE(sbq->wake_batch, wake_batch);
  382. }
  383. void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq,
  384. unsigned int users)
  385. {
  386. unsigned int wake_batch;
  387. unsigned int depth = (sbq->sb.depth + users - 1) / users;
  388. wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES,
  389. 1, SBQ_WAKE_BATCH);
  390. WRITE_ONCE(sbq->wake_batch, wake_batch);
  391. }
  392. EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch);
  393. void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
  394. {
  395. sbitmap_queue_update_wake_batch(sbq, depth);
  396. sbitmap_resize(&sbq->sb, depth);
  397. }
  398. EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
  399. int __sbitmap_queue_get(struct sbitmap_queue *sbq)
  400. {
  401. return sbitmap_get(&sbq->sb);
  402. }
  403. EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
  404. unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
  405. unsigned int *offset)
  406. {
  407. struct sbitmap *sb = &sbq->sb;
  408. unsigned int hint, depth;
  409. unsigned long index, nr;
  410. int i;
  411. if (unlikely(sb->round_robin))
  412. return 0;
  413. depth = READ_ONCE(sb->depth);
  414. hint = update_alloc_hint_before_get(sb, depth);
  415. index = SB_NR_TO_INDEX(sb, hint);
  416. for (i = 0; i < sb->map_nr; i++) {
  417. struct sbitmap_word *map = &sb->map[index];
  418. unsigned long get_mask;
  419. unsigned int map_depth = __map_depth(sb, index);
  420. sbitmap_deferred_clear(map);
  421. if (map->word == (1UL << (map_depth - 1)) - 1)
  422. goto next;
  423. nr = find_first_zero_bit(&map->word, map_depth);
  424. if (nr + nr_tags <= map_depth) {
  425. atomic_long_t *ptr = (atomic_long_t *) &map->word;
  426. unsigned long val;
  427. get_mask = ((1UL << nr_tags) - 1) << nr;
  428. val = READ_ONCE(map->word);
  429. while (!atomic_long_try_cmpxchg(ptr, &val,
  430. get_mask | val))
  431. ;
  432. get_mask = (get_mask & ~val) >> nr;
  433. if (get_mask) {
  434. *offset = nr + (index << sb->shift);
  435. update_alloc_hint_after_get(sb, depth, hint,
  436. *offset + nr_tags - 1);
  437. return get_mask;
  438. }
  439. }
  440. next:
  441. /* Jump to next index. */
  442. if (++index >= sb->map_nr)
  443. index = 0;
  444. }
  445. return 0;
  446. }
  447. int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  448. unsigned int shallow_depth)
  449. {
  450. WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
  451. return sbitmap_get_shallow(&sbq->sb, shallow_depth);
  452. }
  453. EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow);
  454. void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
  455. unsigned int min_shallow_depth)
  456. {
  457. sbq->min_shallow_depth = min_shallow_depth;
  458. sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
  459. }
  460. EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
  461. static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
  462. {
  463. int i, wake_index;
  464. if (!atomic_read(&sbq->ws_active))
  465. return;
  466. wake_index = atomic_read(&sbq->wake_index);
  467. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  468. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  469. /*
  470. * Advance the index before checking the current queue.
  471. * It improves fairness, by ensuring the queue doesn't
  472. * need to be fully emptied before trying to wake up
  473. * from the next one.
  474. */
  475. wake_index = sbq_index_inc(wake_index);
  476. /*
  477. * It is sufficient to wake up at least one waiter to
  478. * guarantee forward progress.
  479. */
  480. if (waitqueue_active(&ws->wait) &&
  481. wake_up_nr(&ws->wait, nr))
  482. break;
  483. }
  484. if (wake_index != atomic_read(&sbq->wake_index))
  485. atomic_set(&sbq->wake_index, wake_index);
  486. }
  487. void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
  488. {
  489. unsigned int wake_batch = READ_ONCE(sbq->wake_batch);
  490. unsigned int wakeups;
  491. if (!atomic_read(&sbq->ws_active))
  492. return;
  493. atomic_add(nr, &sbq->completion_cnt);
  494. wakeups = atomic_read(&sbq->wakeup_cnt);
  495. do {
  496. if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch)
  497. return;
  498. } while (!atomic_try_cmpxchg(&sbq->wakeup_cnt,
  499. &wakeups, wakeups + wake_batch));
  500. __sbitmap_queue_wake_up(sbq, wake_batch);
  501. }
  502. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
  503. static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
  504. {
  505. if (likely(!sb->round_robin && tag < sb->depth))
  506. data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag);
  507. }
  508. void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset,
  509. int *tags, int nr_tags)
  510. {
  511. struct sbitmap *sb = &sbq->sb;
  512. unsigned long *addr = NULL;
  513. unsigned long mask = 0;
  514. int i;
  515. smp_mb__before_atomic();
  516. for (i = 0; i < nr_tags; i++) {
  517. const int tag = tags[i] - offset;
  518. unsigned long *this_addr;
  519. /* since we're clearing a batch, skip the deferred map */
  520. this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word;
  521. if (!addr) {
  522. addr = this_addr;
  523. } else if (addr != this_addr) {
  524. atomic_long_andnot(mask, (atomic_long_t *) addr);
  525. mask = 0;
  526. addr = this_addr;
  527. }
  528. mask |= (1UL << SB_NR_TO_BIT(sb, tag));
  529. }
  530. if (mask)
  531. atomic_long_andnot(mask, (atomic_long_t *) addr);
  532. smp_mb__after_atomic();
  533. sbitmap_queue_wake_up(sbq, nr_tags);
  534. sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(),
  535. tags[nr_tags - 1] - offset);
  536. }
  537. void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
  538. unsigned int cpu)
  539. {
  540. /*
  541. * Once the clear bit is set, the bit may be allocated out.
  542. *
  543. * Orders READ/WRITE on the associated instance(such as request
  544. * of blk_mq) by this bit for avoiding race with re-allocation,
  545. * and its pair is the memory barrier implied in __sbitmap_get_word.
  546. *
  547. * One invariant is that the clear bit has to be zero when the bit
  548. * is in use.
  549. */
  550. smp_mb__before_atomic();
  551. sbitmap_deferred_clear_bit(&sbq->sb, nr);
  552. /*
  553. * Pairs with the memory barrier in set_current_state() to ensure the
  554. * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
  555. * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
  556. * waiter. See the comment on waitqueue_active().
  557. */
  558. smp_mb__after_atomic();
  559. sbitmap_queue_wake_up(sbq, 1);
  560. sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
  561. }
  562. EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
  563. void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
  564. {
  565. int i, wake_index;
  566. /*
  567. * Pairs with the memory barrier in set_current_state() like in
  568. * sbitmap_queue_wake_up().
  569. */
  570. smp_mb();
  571. wake_index = atomic_read(&sbq->wake_index);
  572. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  573. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  574. if (waitqueue_active(&ws->wait))
  575. wake_up(&ws->wait);
  576. wake_index = sbq_index_inc(wake_index);
  577. }
  578. }
  579. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
  580. void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
  581. {
  582. bool first;
  583. int i;
  584. sbitmap_show(&sbq->sb, m);
  585. seq_puts(m, "alloc_hint={");
  586. first = true;
  587. for_each_possible_cpu(i) {
  588. if (!first)
  589. seq_puts(m, ", ");
  590. first = false;
  591. seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i));
  592. }
  593. seq_puts(m, "}\n");
  594. seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
  595. seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
  596. seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
  597. seq_puts(m, "ws={\n");
  598. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  599. struct sbq_wait_state *ws = &sbq->ws[i];
  600. seq_printf(m, "\t{.wait=%s},\n",
  601. waitqueue_active(&ws->wait) ? "active" : "inactive");
  602. }
  603. seq_puts(m, "}\n");
  604. seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin);
  605. seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
  606. }
  607. EXPORT_SYMBOL_GPL(sbitmap_queue_show);
  608. void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
  609. struct sbq_wait_state *ws,
  610. struct sbq_wait *sbq_wait)
  611. {
  612. if (!sbq_wait->sbq) {
  613. sbq_wait->sbq = sbq;
  614. atomic_inc(&sbq->ws_active);
  615. add_wait_queue(&ws->wait, &sbq_wait->wait);
  616. }
  617. }
  618. EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
  619. void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
  620. {
  621. list_del_init(&sbq_wait->wait.entry);
  622. if (sbq_wait->sbq) {
  623. atomic_dec(&sbq_wait->sbq->ws_active);
  624. sbq_wait->sbq = NULL;
  625. }
  626. }
  627. EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
  628. void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
  629. struct sbq_wait_state *ws,
  630. struct sbq_wait *sbq_wait, int state)
  631. {
  632. if (!sbq_wait->sbq) {
  633. atomic_inc(&sbq->ws_active);
  634. sbq_wait->sbq = sbq;
  635. }
  636. prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
  637. }
  638. EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
  639. void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
  640. struct sbq_wait *sbq_wait)
  641. {
  642. finish_wait(&ws->wait, &sbq_wait->wait);
  643. if (sbq_wait->sbq) {
  644. atomic_dec(&sbq->ws_active);
  645. sbq_wait->sbq = NULL;
  646. }
  647. }
  648. EXPORT_SYMBOL_GPL(sbitmap_finish_wait);